首页 > 解决方案 > 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

标签: kotlin

解决方案


您的代码的问题是 data 参数不可为空,但 Android 使用 null 值调用它。

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

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

与你所拥有的相反

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

不同的是?在“数据:意图”之后添加?并对您的代码进行适当的更改(以考虑可为空的值),它应该可以工作。


推荐阅读