我正在尝试使用FirebaseAuth从当前经过身份验证的用户检索并显示用户名,但是应用程序崩溃,并显示NullPointerException
我试图在consol中显示user.getDisplayName()而不显示为空
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.
答案 0 :(得分:0)
空指针尝试说您的Textview为空。之所以会发生这种情况,是因为findviewbyid与您XML上的正确对象不匹配。确保您的ID与XML和.Java Java文件匹配
答案 1 :(得分:0)
您必须在调用该对象的方法中使用线程。
答案 2 :(得分:0)
'nameView'或'emailView'变量为null。请检查R.id.name和R.id.email在当前视图xml中是否为有效的TextView。