onActivityResult: 传递结果失败 ResultInfo

onActivityResult: Failure delivering result ResultInfo

我正在尝试设置 google 登录我的登录 activity 但我在 onActivityResult

上收到此错误
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=9001, result=-1, data=Intent { (has extras) }}

我检查并尝试了许多针对此错误的解决方案,这些解决方案已发布在其他问题上,但根本没有帮助!

这里是我如何在 activity class

中使用它
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)

这是 activity 结果

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 

            }
        }
    }

这里是错误的完整 logcat

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

这里是 Presenter 中的 result 方法和 Interactor 中的附加方法 class

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

一般来说,您不应在代码中使用 !!。选择双感叹号是为了向你尖叫,就像在 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)

注意通话记录是从下到上的,从你的activity第73行我们进入mPresenter第34行,然后到其他classc.d.a.a.a.d.a 第 66 行实际发生 NullPointerException 的地方。 您应该遵循此日志并修复第三个 class.

中的空问题