获取kotlin代码的RuntimeException

时间:2018-06-18 08:38:14

标签: kotlin

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


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) 

代码在这里

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)
    }

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

1 个答案:

答案 0 :(得分:0)

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

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

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

与你拥有的相反

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

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