我想了2天解决这个问题。我做了一个片段,我在其中开启了启用和禁用蓝牙的功能。那么在我的主要活动我写道:Switch findViewById返回null
bluetoothSwitch = (Switch) findViewById(R.id.bluetooth_switch);
bluetoothSwitch.setOnClickListener(clicked);
其中点击:
clicked = new ButtonClicked();
和:
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回空...你有什么想法???
“R.id.bluetooth_switch”在哪里?在你的Activity或Fragment中。因为现在你正在搜索活动。 –
显示您的xml文件以获取您的片段,并且活动帽包含它 – user3793589