Kotlin - IllegalArgumentException in overridden method

这一生的挚爱 提交于 2021-01-02 05:20:16

问题


In Kotlin I'm overriding this two Google Sign-In functions:

override fun onConnectionFailed(result: ConnectionResult) {
        if (result.hasResolution()) {
            try {
                result.startResolutionForResult(this, RESOLVE_CONNECTION_REQUEST_CODE)
            } catch (e: IntentSender.SendIntentException) {
                // Unable to resolve, message user appropriately
            }

        } else {
            val gaa = GoogleApiAvailability.getInstance()
            gaa.getErrorDialog(this, result.errorCode, 0)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
        when (requestCode) {
            RESOLVE_CONNECTION_REQUEST_CODE -> if (resultCode == Activity.RESULT_OK) {
                mGoogleApiClient!!.connect()
            }
        }
    }

to check if the connection to google fails.

The problem is that sometimes, when I dismiss the dialog containing user accounts, which pop-up when the activity is launched

Like this one:

I'm getting an IllegalArgumentException with the following logcat

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.dancam.subscriptions, PID: 6346 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=6783, result=0, data=null} to activity {com.dancam.subscriptions/com.dancam.subscriptions.ActiveSubscriptions.Subscriptions_main}: 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:4126) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4169) at android.app.ActivityThread.-wrap20(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1552) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6186) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter data at com.dancam.subscriptions.ActiveSubscriptions.Subscriptions_main.onActivityResult(Subscriptions_main.kt:0) at android.app.Activity.dispatchActivityResult(Activity.java:6937) at android.app.ActivityThread.deliverResults(ActivityThread.java:4122) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4169)  at android.app.ActivityThread.-wrap20(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1552)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6186)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) 

that points to the onActivityResult function.

I tried changing

requestCode: Int

to

requestCode: Int?

But then I obviously get an error because the function is not the same as the one from it's superclass.

How can I fix it?


回答1:


you need to mention data as null so do data: Intent? because data intent can be null when action is cancelled or no result was sent

Failure delivering result ResultInfo{who=null, request=6783, result=0, data=null} to activity Parameter specified as non-null is null:

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


来源:https://stackoverflow.com/questions/47226041/kotlin-illegalargumentexception-in-overridden-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!