且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果用户未先单击Firebase身份验证发送的验证链接,如何防止用户登录android应用程序?

更新时间:2023-01-09 17:29:28

NullPointerException 是由以下代码引起的:

The NullPointerException is caused by this code:

public class StartActivity extends AppCompatActivity {

    MaterialButton goToLogin, goToRegister;
    ImageView banner;

    FirebaseUser firebaseUser;
    FirebaseAuth auth;

    @Override
    protected void onStart() {
        super.onStart();

        firebaseUser = FirebaseAuth.getInstance().getCurrentUser();

        //redirect if user is not null
        if (Objects.requireNonNull(auth.getCurrentUser()).isEmailVerified()) {

在调用 auth.getCurrentUser()之前,您永远不会初始化 auth ,这就是为什么您得到 NullPointerException 的原因.简单的解决方法是初始化 auth :

You never initialize auth before the call to auth.getCurrentUser(), which is why you get a NullPointerException. The simple fix is to initialize auth:

protected void onStart() {
    super.onStart();

    auth = FirebaseAuth.getInstance();

    firebaseUser = auth.getCurrentUser();

    //redirect if user is not null
    if (Objects.requireNonNull(auth.getCurrentUser()).isEmailVerified()) {
        ...

但我也强烈建议您阅读什么是NullPointerException,以及如何解决?,因为这种类型的错误非常普遍,可以按照此处概述的方法轻松解决.

But I also highly recommend reading What is a NullPointerException, and how do I fix it?, as this type of error is quite common and an easily be fixed by following the approach outlined there.