获取 kotlin 代码的运行时异常



我正在使用蓝牙 LE 来获取周围的设备,但我没有得到列表,而且我得到了运行时异常,这里是堆栈跟踪

06-18 13:
59:33.188 10189-10189/com.labs.aress.bleomronkotlin E/Android运行时:致命异常:main 流程: com.labs.aress.bleomronkotlin, PID: 10189 java.lang.RuntimeException: Failure Delivery result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.labs.aress.bleomronkotlin/com.labs.aress.bleomronkotlin.MainActivity}: java.lang.IllegalArgumentException: 指定为非空的参数为空: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter data at android.app.ActivityThread.deliverResults(ActivityThread.java:4094( at android.app.ActivityThread.handleSendResult(ActivityThread.java:4137( at android.app.ActivityThread.-wrap20(ActivityThread.java( at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1529( at android.os.Handler.dispatchMessage(Handler.java:102( at android.os.Looper.loop(Looper.java:154( at android.app.ActivityThread.main(ActivityThread.java:6123( at java.lang.reflect.Method.invoke(Native Method( at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867( at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757(  由以下原因引起:java.lang.IllegalArgumentException:指定为非 null 的参数为 null:method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull,参数数据 at com.labs.aress.bleomronkotlin.BaseMainActivity.onActivityResult(BaseMainActivity.kt( at android.app.Activity.dispatchActivityResult(Activity.java:6931( at android.app.ActivityThread.deliverResults(ActivityThread.java:4090( at android.app.ActivityThread.handleSendResult(ActivityThread.java:4137( at android.app.ActivityThread.-wrap20(ActivityThread.java( at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1529( at android.os.Handler.dispatchMessage(Handler.java:102( at android.os.Looper.loop(Looper.java:154( at android.app.ActivityThread.main(ActivityThread.java:6123( at java.lang.reflect.Method.invoke(Native Method( at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867( at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757(

代码在这里

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
AppLog.dMethodIn()
super.onActivityResult(requestCode, resultCode, data)
if (REQUEST_CODE_SCAN != requestCode) {
AppLog.w("Unknown request code : $requestCode")
return
}
if (BleScanActivity.RESPONSE_CODE_CONNECT != resultCode) {
return
}
val discoverPeripheral = data.getParcelableExtra<DiscoverPeripheral?>(BleScanActivity.EXTRA_CONNECT_REQUEST_PERIPHERAL)
if (null == discoverPeripheral) {
AppLog.e("null == discoverPeripheral")
return
}
initDeviceInfoView()
initConnectionView()
onConnect(discoverPeripheral)
}

我不明白为什么数据会抛出空

代码的问题在于数据参数不可为空,但 Android 使用空值调用它。

如果你在Android Studio中覆盖onActivityResult函数,你得到的函数签名实际上是

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)

与你所拥有的相反

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent)

区别在于"数据:意图"之后的?。 添加 ?并对代码进行适当的更改(以考虑可为 null 的值(,它应该可以工作。

最新更新