简体   繁体   English

Java、Android、Firebase 身份验证登出抛出 NullPointerException

[英]Java, Android, Firebase Authentication signin out throws NullPointerException

I have an app with email / password firebase authentication.我有一个带有电子邮件/密码 firebase 身份验证的应用程序。

It starts with the login activity它从登录活动开始

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    mAuth = FirebaseAuth.getInstance();
    ...

Then in my onStart , I check if the currentuser isn't null, then it navigates to my main activity.然后在我的onStart 中,我检查currentuser是否不为空,然后导航到我的主要活动。 This part is working perfectly fine.这部分工作得很好。

@Override
public void onStart() {
    super.onStart();
    FirebaseUser currentUser = mAuth.getCurrentUser();
    if (currentUser != null) {
        goMain();
    }
}

And here comes the problem, in the main activity I have a button where I call the signout function from LoginActivity.java:问题来了,在主活动中,我有一个按钮,我可以在其中调用 LoginActivity.java 的注销函数:

public void logOut() {
    LoginActivity login = new LoginActivity();
    login.logMeOut();
}


public void logMeOut() {
    FirebaseUser user = mAuth.getCurrentUser();
    if(user!=null) {
        mAuth.signOut();
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
    }
}

Problem is when logMeOut runs, I get this beauty问题是当logMeOut运行时,我得到了这个美丽

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()”

I have tried to add mAuth = FirebaseAuth.getInstance() to the start of the logMeOut function, that still didn't solve my problem.我试图将mAuth = FirebaseAuth.getInstance()添加到logMeOut函数的开头,但仍然没有解决我的问题。

If I only call mAuth.signOut() , without the FirebaseUser part, I get the same error.如果我只调用mAuth.signOut()而没有FirebaseUser部分,我会得到同样的错误。

You are creating a new Login activity in your logout method could that be an issue?您正在注销方法中创建新的登录活动,这可能是一个问题吗?

It seems to me the Auth objects would be re initialized right there.在我看来,Auth 对象将在那里重新初始化。 I do my logout the exact same way as you but it is within the main activity in a menu我以与您完全相同的方式注销,但它位于菜单的主要活动中

 case R.id.sign_out_menu:
            mFirebaseAuth.signOut();
            Auth.GoogleSignInApi.signOut(mGoogleApiClient);

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

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