gpt4 book ai didi

java - 如何修复空指针异常 Firebase Auth

转载 作者:行者123 更新时间:2023-12-01 23:41:11 27 4
gpt4 key购买 nike

我尝试使用 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.

最佳答案

空指针尝试说你的 Textview 为空。发生这种情况的原因是 findviewbyid 与 XML 上的正确 ID 不匹配。确保您的 id 与您的 XML 和 .Java java 文件相匹配

关于java - 如何修复空指针异常 Firebase Auth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58259410/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com