【问题标题】:How to fix Null pointer exception Firebase Auth如何修复空指针异常 Firebase Auth
【发布时间】:2019-10-06 16:53:24
【问题描述】:

我正在尝试使用 FirebaseAuth 从当前经过身份验证的用户中检索并显示用户名,但应用程序崩溃并出现 NullPointerException

我尝试在控制台中显示 user.getDisplayName() 而不是 Null

ImageView profilePicView = findViewById(R.id.profile_pic);
        TextView nameView = findViewById(R.id.name);
        TextView emailView = findViewById(R.id.email);

        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        if (user != null) {
            if (user.getDisplayName() != null) {
                System.out.println("------------------------------user display name : " + user.getDisplayName());
                nameView.setText(user.getDisplayName());
            }

            if (user.getEmail() != null) {
                emailView.setText(user.getEmail());
            }

            if (user.getPhotoUrl() != null) {
                Glide.with(this)
                        .load(user.getPhotoUrl())
                        .into(profilePicView);
            }
        }

这是堆栈错误

I/System.out: ------------------------------user display name : Mohammed Elmahdi Bechlaghem
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.megadevbrain.ranahna, PID: 20913
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.megadevbrain.ranahna/com.megadevbrain.ranahna.Navigation}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3121)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3264)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7076)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.megadevbrain.ranahna.Navigation.onCreate(Navigation.java:52)
        at android.app.Activity.performCreate(Activity.java:7327)
        at android.app.Activity.performCreate(Activity.java:7318)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3101)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3264) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1955) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7076) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965) 
I/Process: Sending signal. PID: 20913 SIG: 9
Process 20913 terminated.

【问题讨论】:

  • 确保您的xml 中有testview,ID 为name。或发布您的xml 文件。

标签: java android firebase firebaseui


【解决方案1】:

空指针试图说您的 Textview 为空。这可能是因为 findviewbyid 与您的 XML 上的正确的不匹配。确保您的 id 与您的 XML 和 .Java java 文件匹配

【讨论】:

  • 这对我很有帮助,我在使用导航抽屉时没有 "getHeaderView(0).findViewById(R.id.profile_pic)" ,完全错过了,感谢您的帮助。
【解决方案2】:

您必须在调用对象的方法中使用 Thread。

【讨论】:

    【解决方案3】:

    “nameView”或“emailView”变量为空。请检查 R.id.name 和 R.id.email 在当前视图 xml 中是否为有效的 TextView。

    【讨论】:

      猜你喜欢
      • 2023-03-15
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多