2016-07-25 2 views
0

이 문제를 해결하는 데 2 ​​일이 걸립니다. 나는 블루투스를 켜고 끌 수있는 스위치를 만든 단편을 만들었습니다. 그럼 내 주요 활동에서 내가 쓴 :Switch findViewById returns null

clicked = new ButtonClicked(); 

과 : 클릭

bluetoothSwitch = (Switch) findViewById(R.id.bluetooth_switch); 
bluetoothSwitch.setOnClickListener(clicked); 

class ButtonClicked implements AdapterView.OnClickListener 
{ 
    @Override 
    public void onClick(View view) { 
     switch (view.getId()) { 
      case R.id.bluetooth_switch: 
       if (bluetoothAdapter.isEnabled()){ 
        bluetoothAdapter.disable(); 
        Log.d("Log", "Bluetooth is disabled"); 
       } 
       else{ 
        bluetoothAdapter.enable(); 
        Log.d("Log", "Bluetooth is enabled"); 
       } 
       break; 
      case R.id.buttonSearch: 
       arrayListBluetoothDevices.clear(); 
       startSearching(); 
       break; 
      case R.id.discoverable_switch: 
       makeDiscoverable(); 
       break; 
      default: 
       break; 
     } 
    } 
} 

을 나는 그것에게 findViewById를 반환 널 (null)을 실행할 때 ... 당신이 어떤 생각을 가지고 있습니까 ???

+0

'R.id.bluetooth_switch'는 정확히 어디에 있습니까? 귀하의 활동 또는 단편. 왜냐하면 지금 당신은 활동을 찾고 있습니다. –

+0

조각에 대한 xml 파일을 표시하고 활동 모자에 포함되어 있습니다. – user3793589

답변

1

당신이하려는 일은 작동하지 않습니다. findViewByID은 활동 xml 내에서보기를 찾지 만보기 (R.id.bluetooth_switch)는 조각의 xml 안에 있습니다.
코드 구현에 대한 답변은 this입니다. 제공된 ID 내가 코드 줄을 이동 그것을 해결 현재 컨테이너

0

"같이 findViewById는"null를 돌려줍니다 내 조각 자바 파일. 감사.

0

의 "자원 트리"에 존재하지 않는 (귀하의 예제에서, "bluetooth_switch"라고) 때