Kotlin - 重写方法中的 IllegalArgumentException
Kotlin - IllegalArgumentException in overridden method
在 Kotlin 中,我重写了这两个 Google 登录函数:
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()
}
}
}
检查与 google 的连接是否失败。
问题是有时,当我关闭包含用户帐户的对话框时,该对话框会在 activity 启动时弹出
喜欢这个:
我得到一个 IllegalArgumentException
具有以下 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)
指向 onActivityResult
函数。
我试过改变
requestCode: Int
到
requestCode: Int?
但是我显然得到了一个错误,因为这个函数与它的超类中的函数不一样。
我该如何解决?
你需要提到 data
和 null
一样 data: Intent?
因为 data
意图可以是 null
当行动被取消或没有发送结果时
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?) {
// ^^
在 Kotlin 中,我重写了这两个 Google 登录函数:
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()
}
}
}
检查与 google 的连接是否失败。
问题是有时,当我关闭包含用户帐户的对话框时,该对话框会在 activity 启动时弹出
喜欢这个:
我得到一个 IllegalArgumentException
具有以下 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)
指向 onActivityResult
函数。
我试过改变
requestCode: Int
到
requestCode: Int?
但是我显然得到了一个错误,因为这个函数与它的超类中的函数不一样。
我该如何解决?
你需要提到 data
和 null
一样 data: Intent?
因为 data
意图可以是 null
当行动被取消或没有发送结果时
Failure delivering result ResultInfo{who=null, request=6783, result=0,
data=null
} to activity Parameter specified asnon-null is null
:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
// ^^