public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
BluetoothManager btManager = new BluetoothManager(this);
setContentView(R.layout.main);
if (!btManager.isEnabled()) {
btManager.requestBluetoothEnable();
discoveryStarted = false;
} else {
if (btManager.isDeviceSetNullOrEmpty()) {
btManager.startDiscovery();
discoveryStarted = true;
}
}
}
@Override
public void onResume(){
super.onResume();
if( btManager.isEnabled() && discoveryStarted == false ){
即使我初始化了我的 btManager 类(一些蓝牙类的包装器),我还是在这里遇到 NullPointer 异常。
最佳答案
BluetoothManager btManager = new BluetoothManager(this);
仅在 onCreate 中本地声明。
尝试将 BlueToothManager btManager;
作为类范围的字段(就在类声明下方)。然后,在 oncreate 中:
btManager = new BluetoothManager(this);
然后看看在onResume里面不带空指针能不能访问到!
关于android - 我试图弄清楚为什么当我的应用程序从 onCreate 转换到 onResume 时我会收到 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10906643/