简体   繁体   English

获取记录的当前用户电子邮件

[英]get current user email that is logged

I am using firebase to do the authentication in my application, and everything works fine, but when i log in i want to display the user email that is logged in, and what i did crashes the application when i run it, so i did this: 我正在使用Firebase在应用程序中进行身份验证,并且一切正常,但是当我登录时,我想显示已登录的用户电子邮件,并且在运行该应用程序时我崩溃了,所以我这样做了:

 auth = FirebaseAuth.getInstance();
  userTxt.setText(auth.getCurrentUser().getEmail().toString());

i defined a auth object and then used that auth to get the current user email, after that all in one line i passed that to my textView, what am i doing wrong here? 我定义了一个auth对象,然后使用该auth获取当前用户的电子邮件,在所有这些中,我将其传递给了textView,在这里我在做什么错?

StackTrace 堆栈跟踪

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) 

it crash because getCurrentUser() might return null 它崩溃,因为getCurrentUser()可能返回null

try this 尝试这个

    mAuth = FirebaseAuth.getInstance();
    final FirebaseUser theUser = mAuth.getCurrentUser();
    if (theUser !=null)
    String _UID = theUser.getEmail().toString();

and I prefer to use AuthStateListener 我更喜欢使用AuthStateListener

In MainActivity MainActivity

    private FirebaseAuth mAuth;
    private FirebaseAuth.AuthStateListener mAuthListener;

then in onCreate 然后在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
                }
            }
        };

and in onStart 并在onStart

@Override
public void onStart() {
    super.onStart();
      mAuth.addAuthStateListener(mAuthListener);
}

and in onStop 并在onStop

@Override
public void onStop() {
    super.onStop();
    if (mAuthListener != null) {
        mAuth.removeAuthStateListener(mAuthListener);
    }
}

Your logcat is giving you the answer: 您的logcat给您答案:

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 原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()'

so your invoking getCurrentUser() on a null object. 因此您在空对象上调用getCurrentUser()

extra: 额外:

I have just come upon another situation where the user's email would return null even if you have correctly initialized your FirebaseUser 我刚刚遇到另一种情况,即使您已正确初始化FirebaseUser ,用户的电子邮件也将返回null

There's a closed bug report already filed with google: 谷歌已经提交了一份封闭的错误报告

When you go to the Auth > Sign-In Methods page of your project in the Firebase console. 当您在Firebase控制台中转到项目的“身份验证”>“登录方法”页面时。 do you have One account per email address on or off? 每个电子邮件地址是否有一个帐户 If you allow multiple accounts per email address, you will get null for FirebaseUser. 如果您每个电子邮件地址允许多个帐户, FirebaseUser 将为空 getEmail() getEmail()

在此处输入图片说明

You have to use the option: "Prevent creation of multiple accounts with the same email address" or else FirebaseUser.getEmail() will return null 您必须使用以下选项:“防止创建具有相同电子邮件地址的多个帐户”,否则FirebaseUser.getEmail()将返回null

Ps: only users who first logged in after the option was turned off will be able to use this method successfully 附言:只有在关闭该选项后首次登录的用户才能成功使用此方法

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

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