Kotlin – 重写方法中的IllegalArgumentException

在Kotlin中,我重写了这两个Google Sign-In函数:

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

检查是否连接到谷歌失败。

问题是,有时,当我关闭包含用户帐户的对话框时,在活动启动时popup

像这个:

对话

我得到一个IllegalArgumentException与下面的logcat

E / AndroidRuntime:致命例外:主进程:com.dancam.subscriptions,PID:6346 java.lang.RuntimeException:失败的结果ResultInfo {who = null,request = 6783,result = 0,data = null} to activity {com .dancam.subscriptions / com.dancam.subscriptions.ActiveSubscriptions.Subscriptions_main}:java.lang.IllegalArgumentException:指定为非null的参数为null:方法kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull,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)在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run的java.lang.reflect.Method.invoke(Native Method) (ZygoteInit.java:889)com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)引起:java.lang.IllegalArgumentException:指定为非null的参数为null:方法kotlin.jvm.internal .Intrinsics.checkParameterIsNotNull,参数数据位于android.app.Activity.dispatchActivityResult(Activity.java:6937)上的android.app.ActivityThread.deliverResults()的com.dancam.subscriptions.ActiveSubscriptions.Subscriptions_main.onActivityResult(Subscriptions_main.kt:0) (ActivityThread.java:4129)在android.app.ActivityThread.-wrap20(ActivityThread.java)android.app.ActivityThread $ H.handleMessage(ActivityThread.java:ActivityThread.java:4122)在android.app.ActivityThread.handleSendResult(ActivityThread.java:4169) 1552)在android.os.Handler.dispatchMessage(Handler.java:102)在android.os.Looper.loop(Looper.java:154)在android.app.ActivityThread.main(ActivityThread.java:6186)在java。 lang.reflect.Method.invoke(Native Method)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:889)at c om.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

指向onActivityResult函数。

我试图改变

 requestCode: Int 

 requestCode: Int? 

但是,我明显得到一个错误,因为这个函数和它的超类不一样。

我该如何解决?

    你需要提及datanull所以data: Intent? 因为在取消操作或未发送结果时, data意图可能为null

    失败的结果ResultInfo {who = null,request = 6783,result = 0, data=null }给activity指定non-null is null参数non-null is null

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