在调用 launch() 之前必须确保 ActivityResultLauncher 已注册

问题描述 投票:0回答:1

在一些 android 设备中,我在 ActivityResultLauncher

中得到这个 IllegalStateException
Fatal Exception: java.lang.IllegalStateException: Attempting to launch an unregistered ActivityResultLauncher with contract androidx.activity.result.contract.ActivityResultContracts$StartActivityForResult@3325833 and input Intent { act=com.google.android.gms.auth.GOOGLE_SIGN_IN pkg=com.xyz.app cmp=com.xyz.app/com.google.android.gms.auth.api.signin.internal.SignInHubActivity (has extras) }. You must ensure the ActivityResultLauncher is registered before calling launch().
       at androidx.activity.result.ActivityResultRegistry$2.launch(ActivityResultRegistry.java:168)
       at androidx.activity.result.ActivityResultLauncher.launch(ActivityResultLauncher.java:47)
       at com.xyz.app.ui.auth.login.LoginActivity.doAuth$lambda$22$lambda$20(LoginActivity.kt:318)
       at com.xyz.app.ui.auth.login.LoginActivity.$r8$lambda$zt4kZAeP81QjQuIivycpvJCWE4o()
       at com.xyz.app.ui.auth.login.LoginActivity$$ExternalSyntheticLambda7.onComplete(:4)
       at com.google.android.gms.tasks.zzi.run(com.google.android.gms:play-services-tasks@@18.0.1:1)
       at android.os.Handler.handleCallback(Handler.java:790)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:214)
       at android.app.ActivityThread.main(ActivityThread.java:6977)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:528)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:910)

我在 onCreate() 方法中注册了启动器。 当 Activity 由于配置更改(例如方向更改时)而被销毁和重新创建时,我在 onCreate 中重新初始化 ActivityResultLauncher,然后再次调用启动,并在启动之前进行 Null 检查。

private lateinit var resultLauncherForGmailSignIn: ActivityResultLauncher<Intent>

public class LoginActivity extends AppCompatActivity {
    
    private ActivityResultLauncher<Intent> myLauncher;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
         resultLauncherForGmailSignIn =
            registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
                val intentData: Intent? = result.data
                intentData?.let {
                    loginViewModel.signInWithGmail(it)
                }
            }
      
        findViewById(R.id.myButton).setOnClickListener(v -> {
                    if (this::resultLauncherForGmailSignIn.isInitialized) {
                        resultLauncherForGmailSignIn.launch(signInIntent)
                    }
        });
    }
}
android illegalstateexception
1个回答
0
投票

嗯,你自己回答了。您在第二个

register
电话中呼叫
onCreate
。那是错误的。

你可以将它注册为 Activity 类中的变量,比如

class MyActiviy : AppCompatActivity() {

    val resultLauncherForGmailSignIn: ActivityResultLauncher<Intent> = registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()) {
        ...
    }

这将确保寄存器仅在creation时调用一次,但不会用于重新创建。

或者您可以检查

savedInstanceState
参数,在recreate场景的情况下它不会为空。

© www.soinside.com 2019 - 2024. All rights reserved.