【发布时间】:2021-06-18 10:47:45
【问题描述】:
我正在尝试在我的登录活动中设置谷歌登录,但我在 onActivityResult 上收到此错误
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=9001, result=-1, data=Intent { (has extras) }}
我检查并尝试了许多针对此错误的解决方案,这些解决方案已发布在其他问题上,但根本没有帮助!
这里是我如何在我的活动课程中使用它
private lateinit var googleSignInClient: GoogleSignInClient
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(GOOGLE_CLIENT_ID)
.requestEmail()
.build()
googleSignInClient = GoogleSignIn.getClient(this, gso)
startActivityForResult(googleSignInClient.signInIntent!!, RC_SIGN_IN)
这里是活动结果
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode!=RESULT_CANCELED){
if (requestCode == RC_SIGN_IN && data!=null) {
Log.d(TAG, "onActivityResult $mPresenter : $requestCode : $resultCode : $data")
mPresenter!!.result(requestCode, resultCode, data) // line of the error 73
}
}
}
这里是错误的完整日志
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.googlemvp.signin, PID: 11583
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=9001, result=-1, data=Intent { (has extras) }} to activity {com.googlemvp.signin/com.googlemvp.signin.ui.LoginActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:4846)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4887)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2017)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7397)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
Caused by: java.lang.NullPointerException
at c.d.a.a.a.d.a(:66)
at c.d.a.a.a.e.f(:34)
at com.googlemvp.signin.ui.LoginActivity.onActivityResult(:73)
at android.app.Activity.dispatchActivityResult(Activity.java:8110)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4839)
这里的 onActivityResult 日志中的数据根本不为空
LoginActivity: onActivityResult c.d.a.a.a.e@97eb6e3 : 9001 : -1 : Intent { (has extras) }
这里是演示者中的结果方法和交互器类中的附加方法
// in presenter class
override fun result(requestCode: Int, resultCode: Int, data: Intent?) {
mInteractor!!.performResult(requestCode, resultCode,data)
}
// in interactor class
override fun performResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == RC_SIGN_IN) {
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
try {
// Google Sign In was successful, authenticate with Firebase
val account = task.getResult(ApiException::class.java)!!
mListener!!.onStart(account.idToken!!)
} catch (e: ApiException) {
// Google Sign In failed, update UI appropriately
mListener!!.onFailure(e)
// Log.w(TAG, "Google sign in failed", e)
}
}
}
【问题讨论】:
-
您的presenter为空,请确保已初始化或调用
mPresenter?.let{it.result(requestCode, resultCode, data) ) -
我在日志中打印了演示者对象,它不为空,我从这部分 account.idToken 中发现错误 --> 它为空
标签: android firebase kotlin google-authentication google-signin