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

问题描述 投票:0回答:1
我正在做android应用程序,需要通过蓝牙与特殊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

java android nullpointerexception
1个回答
1
投票
使用return以任何void方法停止代码
© www.soinside.com 2019 - 2024. All rights reserved.