我正在使用firebase在我的应用程序中进行身份验证,一切正常,但是当我登录时,我想要显示登录的用户电子邮件,当我运行它时,我做了什么崩溃应用程序,所以我这样做了:
auth = FirebaseAuth.getInstance();
userTxt.setText(auth.getCurrentUser().getEmail().toString());
我定义了一个auth对象,然后使用该auth来获取当前用户的电子邮件,之后在一行中我将其传递给我的textView,我在这里做错了什么?
栈跟踪
04-09 09:00:11.247 2987-2987/com.esmad.pdm.friendlymanager E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.esmad.pdm.friendlymanager, PID: 2987
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.esmad.pdm.friendlymanager/com.esmad.pdm.friendlymanager.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()' on a null object reference
at com.esmad.pdm.friendlymanager.MainActivity.onCreate(MainActivity.java:34)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
答案 0 :(得分:0)
崩溃,因为getCurrentUser()
可能会返回null
试试这个
mAuth = FirebaseAuth.getInstance();
final FirebaseUser theUser = mAuth.getCurrentUser();
if (theUser !=null)
String _UID = theUser.getEmail().toString();
我更喜欢使用AuthStateListener
在MainActivity
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
然后在onCreate
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
String _UID = Theuser.getUid();
String Uemail= Theuser..getEmail().toString();
} else {
// User is signed out
}
}
};
并在onStart
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
和 onStop
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
答案 1 :(得分:0)
您的logcat正在给您答案:
引起:java.lang.NullPointerException:尝试调用虚拟 方法' com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()'在null 对象参考
所以你在空对象上调用getCurrentUser()
。
额外:
我刚刚遇到另一种情况,即使您已正确初始化FirebaseUser
已经向谷歌提交了closed bug report:
当您转到验证> Firebase控制台中项目的登录方法页面。你有每个电子邮件地址一个帐户打开还是关闭?如果您允许每个电子邮件地址使用多个帐户,则对于FirebaseUser,您将为空。 getEmail()
您必须使用以下选项:"使用相同的电子邮件地址防止创建多个帐户"否则 FirebaseUser.getEmail()将返回null
Ps:只有在关闭选项后首次登录的用户才能成功使用此方法