简体   繁体   English

如何修复 Null 指针异常 Firebase Auth

[英]How to fix Null pointer exception Firebase Auth

I am trying to Retrieve and display the user name from the current authenticated user with FirebaseAuth but the app crash with a NullPointerException我正在尝试使用 FirebaseAuth 从当前经过身份验证的用户中检索并显示用户名,但应用程序崩溃并出现 NullPointerException

I tryed to display the the user.getDisplayName() in the consol and its not Null我试图在控制台中显示 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);
            }
        }

Here is the stack Error这是堆栈错误

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.

Null pointer try to say that your Textview is null. Null 指针尝试说您的 Textview 是 null。 That can happen because the findviewbyid is not matching with the right one on your XML.这可能是因为 findviewbyid 与 XML 上的正确的不匹配。 Be sure your id matches your XML and the.Java java file确保您的 id 与您的 XML 和 .Java java 文件匹配

you must use Thread in your method that calls the object.您必须在调用 object 的方法中使用 Thread。

Either 'nameView' or 'emailView' variable is null. “nameView”或“emailView”变量是 null。 Please check R.id.name and R.id.email are valid TextView in current view xml. Please check R.id.name and R.id.email are valid TextView in current view xml.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM