【问题标题】:onActivityResult: Failure delivering result ResultInfoonActivityResult:传递结果ResultInfo失败
【发布时间】: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


【解决方案1】:

一般来说,您不应在代码中使用!!。选择双感叹号是为了向你尖叫,就像在don't use it!! 中一样。当您使用 !! 时,您基本上禁用了 Kotlin 最好的功能之一 - 空安全性。

但在你的情况下,这不是你问题的根源。

正如我们在日志中看到的,您有一个 mPresenter 实例,引用为 c.d.a.a.a.e@97eb6e3

然后在崩溃日志中我们看到

 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)

请注意,调用日志从底部到顶部,从您的活动第 73 行,我们进入 mPresenter 第 34 行,然后到其他一些类 c.d.a.a.a.d.a 第 66 行,实际发生 NullPointerException。 您应该遵循此日志并修复第三类中的 null 问题。

【讨论】:

  • 非常感谢,这对我有帮助,我在这部分 mListener!!.onStart(account.idToken!!) 中发现了错误,因为 account id token 为 null
猜你喜欢
  • 2014-01-13
  • 2012-04-07
  • 1970-01-01
  • 1970-01-01
  • 2013-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-27
相关资源
最近更新 更多