尝试在空对象引用上调用虚拟方法 'java.util.Set android.bluetooth.BluetoothAdapter.getBondedDevices()'



我正在做安卓应用程序,需要通过蓝牙与特殊的tablo进行通信。

我的java.lang.NullPointerException有一个错误

我已经宣布了我需要的一切。我需要做什么?

Java类

public class SearchTabloActivity extends AppCompatActivity {
ListView listView;
BluetoothAdapter mBluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_tablo);
listView = (ListView)findViewById(R.id.listView);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(getApplicationContext(), "Bluetooth not support!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(SearchTabloActivity.this, MainActivity.class));
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
List<String> s = new ArrayList<String>();
for(BluetoothDevice bt : pairedDevices)
s.add(bt.getName() + "n" + bt.getAddress());
listView.setAdapter(new ArrayAdapter<String>(this, R.layout.activity_search_tablo, s));
}
}

错误

2020-04-24 09:47:56.208 12121-12121/com.kvaksmanyt.exoy E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.kvaksmanyt.exoy, PID: 12121
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kvaksmanyt.exoy/com.kvaksmanyt.exoy.tablo.SearchTabloActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Set android.bluetooth.BluetoothAdapter.getBondedDevices()' on a null object reference

使用return停止任何void方法中的代码

最新更新