【问题标题】:NullPointerException on BluetoothGatt.close() when the gatt object is not null当 gatt 对象不为空时,BluetoothGatt.close() 上的 NullPointerException
【发布时间】: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() 行中的NullPointerExceptionnotdisconnect() 行)中崩溃。当我删除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


【解决方案1】:

这个 NPE 不应该发生,它看起来像其他一些 Threadnulling 你的变量...你在代码 this.gatt = null; 行或 disconnectDevice() 调用中有任何地方吗?

try{}catch{}es 下方尝试,将套接字断开/关闭/关闭打包到try{ 中很常见(但不是出于 NPE 原因)

private void disconnectDevice() {
    ...
    try {
        this.gatt.disconnect();
    }
    catch(Exception ignored){
    }
    try {
        this.gatt.close();
    }
    catch(Exception ignored){
    }
    finally{
        this.gatt = null;
    }
    ...
}

【讨论】:

  • 是的,我在代码的其他部分有this.gatt = null;disconnectDevice()。但是,我只有一个线程。
猜你喜欢
  • 2023-02-03
  • 1970-01-01
  • 2017-05-17
  • 1970-01-01
  • 2017-08-25
  • 2019-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多