【问题标题】:How to fix this error: 'java.lang.Class java.lang.Object.getClass()' on a null object reference如何修复此错误:空对象引用上的“java.lang.Class java.lang.Object.getClass()”
【发布时间】:2021-02-14 06:40:12
【问题描述】:

我正在制作一个 Android 聊天应用程序。用户,如果验证并登录,则通过执行以下代码超越 StartActivity。

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

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

        //redirect if user is not null
        if (Objects.requireNonNull(auth.getCurrentUser()).isEmailVerified()) {
            if (firebaseUser != null) {
                startActivity(new Intent(StartActivity.this, HomeActivity.class));
                finish();
            }
        } else {
            startActivity(new Intent(StartActivity.this, LoginActivity.class));
            Toast.makeText(this, "Verify your email id!", Toast.LENGTH_SHORT).show();
        }
    }

但每当我运行该应用程序时,它会突然关闭并显示此错误。

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
        at com.smproductions.flingg.StartActivity.onStart(StartActivity.java:32)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1425)
        at android.app.Activity.performStart(Activity.java:7825)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3294)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        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:930)

我该如何解决这个问题?

如需更多参考,请查看我的另一个问题,我在其中添加了更多代码 sn-ps,这可能会有所帮助。 How to prevent users from logging in an android app, if they don't click on the verification link sent by Firebase authentication first?

【问题讨论】:

  • 为什么不在if 条件中简单地使用firebaseUser 而不是(Objects.requireNonNull(auth.getCurrentUser())
  • 我试过了,但现在我收到了这个错误:java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.google.firebase.auth.FirebaseUser.isEmailVerified()' on an null object reference
  • 您的应用程序是否在您开始或任何活动时立即崩溃?
  • 是的,它一启动就崩溃并显示错误。
  • 你得到auth.getCurrentUser()null 所以你应该删除这个if 条件if (Objects.requireNonNull(auth.getCurrentUser()).isEmailVerified()) 并且你应该能够移动到else 块,用户将被导航到你的@ 987654332@

标签: android firebase firebase-realtime-database firebase-authentication


【解决方案1】:

在这里,您需要首先检查 auth.getCurrentUser() 是否为空,您说过您将 auth.getCurrentUser() 设为空,这意味着没有用户登录。您可以了解有关 @ 的更多信息987654324@财产here

所以在这里你应该首先检查当前用户,即你的firebaseUser是否为空,所以代码应该是:-

//Check if the firebase user exist or not 
if (firebaseUser != null) {
    if (firebaseUser. isEmailVerified()) {
       // Your user is verified and can navigate to home activity 
   } else {
        // Your user is not verified 
    }
} else { 
    // You don't have a signed in user
}

【讨论】:

    猜你喜欢
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 2019-10-07
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多