【发布时间】:2021-03-05 07:52:39
【问题描述】:
当我尝试关闭 GATT 连接时,我会执行以下操作:
BluetoothGatt gatt;
...
private void disconnectDevice() {
...
if (this.gatt != null) {
this.gatt.disconnect();
this.gatt.close();
this.gatt = null;
}
...
}
但是,在我的三星 Galaxy S4 mini 上,该应用程序在 this.gatt.close(); 行(disconnect() 行中的NullPointerException (not 在disconnect() 行)中崩溃。当我删除this.gatt.close(); 行(但保留disconnect() 行)时,问题就消失了。在我的三星 Galaxy S5 和 Vodafone Smart prime 6 上,我没有遇到这种问题。
但是,Android reference 鼓励我在完成后调用 close() 函数。
这是日志消息:
D/AndroidRuntime(26003): Shutting down VM
W/dalvikvm(26003): threadid=1: thread exiting with uncaught exception (group=0x41813da0)
E/AndroidRuntime(26003): FATAL EXCEPTION: main
E/AndroidRuntime(26003): Process: org.kolodez.MyApp, PID: 26003
E/AndroidRuntime(26003): java.lang.NullPointerException
E/AndroidRuntime(26003): at org.kolodez.MyApp.ServiceMain.disconnectDevice(ServiceMain.java:575)
E/AndroidRuntime(26003): at org.kolodez.MyApp.ServiceMain.access$1800(ServiceMain.java:34)
E/AndroidRuntime(26003): at org.kolodez.MyApp.ServiceMain$MyBinder.disconnectDevice(ServiceMain.java:536)
E/AndroidRuntime(26003): at org.kolodez.MyApp.ActivityMainLayoutMain.connectDisconnect(ActivityMainLayoutMain.java:377)
E/AndroidRuntime(26003): at org.kolodez.MyApp.ActivityMainLayoutMain.access$1100(ActivityMainLayoutMain.java:36)
E/AndroidRuntime(26003): at org.kolodez.MyApp.ActivityMainLayoutMain$ButtonListener.onClick(ActivityMainLayoutMain.java:527)
E/AndroidRuntime(26003): at android.view.View.performClick(View.java:4881)
E/AndroidRuntime(26003): at android.view.View$PerformClick.run(View.java:19592)
E/AndroidRuntime(26003): at android.os.Handler.handleCallback(Handler.java:733)
E/AndroidRuntime(26003): at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime(26003): at android.os.Looper.loop(Looper.java:146)
E/AndroidRuntime(26003): at android.app.ActivityThread.main(ActivityThread.java:5756)
E/AndroidRuntime(26003): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(26003): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(26003): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
E/AndroidRuntime(26003): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
E/AndroidRuntime(26003): at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 785): Force finishing activity org.kolodez.MyApp/.ActivityMain
【问题讨论】:
-
好吧,你不能把它包在一个 try catch 中吗,如果是这样的话,你似乎无能为力
-
没有其他线程。
标签: java android nullpointerexception android-bluetooth bluetooth-gatt