gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:40:49 30 4
gpt4 key购买 nike

我正在做 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

最佳答案

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

关于java - 尝试在空对象引用上调用虚拟方法 'java.util.Set android.bluetooth.BluetoothAdapter.getBondedDevices()',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61402788/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com