kotlin - Getting RuntimeException for kotlin code
问题描述
I am using Bluetooth LE for fetching surrounding devices but I am not getting a list and also I am getting Runtime Exception here is stack-trace
06-18 13:59:33.188 10189-10189/com.labs.aress.bleomronkotlin E/AndroidRuntime: FATAL EXCEPTION: main Process: com.labs.aress.bleomronkotlin, PID: 10189 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.labs.aress.bleomronkotlin/com.labs.aress.bleomronkotlin.MainActivity}: java.lang.IllegalArgumentException: Parameter specified as non-null is null: 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) Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter data 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)
code is here
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)
}
I dont understand why data is throwing null
解决方案
您的代码的问题是 data 参数不可为空,但 Android 使用 null 值调用它。
如果您在 Android Studio 中覆盖 onActivityResult 函数,您得到的函数签名实际上是
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)
与你所拥有的相反
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent)
不同的是?在“数据:意图”之后添加?并对您的代码进行适当的更改(以考虑可为空的值),它应该可以工作。
推荐阅读
- xamarin.forms - Xamarin Forms w/ Mobile Blazor Bindings - 如何在 Blazor 应用程序中为 Android / iOS 项目中定义的类指定依赖项注入
- python - 计算具有上限和下限的 a/b 测试的置信度
- apache-kafka - TwitterSourceConnector 必须是 SourceConnector 的子类
- json - Flutter 中的 JSON 数据解析
- swift - ARKit 中的可追踪自定义锚点
- c# - 从 SQL Server 到 C# 的价值差异
- javascript - 无法在javascript中访问数组索引
- postgresql - Postgresql 不定式循环
- android - 如何在 android studio 中打开我的依赖项
- reactjs - 云环境中的axios超时但不是本地机器