如何解决Firebase身份验证中的空指针异常?

时间:2019-06-22 16:04:53

标签: java android firebase firebase-authentication

我需要与此相关的帮助。每次我运行该应用程序时,每次在登录活动中打开它都会崩溃。请帮忙,即时通讯卡住了。 我遵循了很多教程,我认为代码是相同的,但仍然出现空指针异常。我不知道如何解决这个空指针。

Logcat:

java.lang.NullPointerException: Attempt to invoke interface method 'void com.google.firebase.auth.FirebaseAuth$AuthStateListener.onAuthStateChanged(com.google.firebase.auth.FirebaseAuth)' on a null object reference
    at com.google.firebase.auth.zzi.run(Unknown Source:2)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at com.google.android.gms.internal.firebase_auth.zzj.dispatchMessage(Unknown Source:6)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6718)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

我在登录活动中的代码:

public class Login extends AppCompatActivity {

    private FirebaseAuth mAuth;
    FirebaseAuth.AuthStateListener mAuthListner;

    Button btnlogin;
    TextView btndaftar;
    MaterialEditText mEmail, pass;

    @Override
    protected void onStart() {
        super.onStart();
        FirebaseUser currentUser = mAuth.getCurrentUser();
        updateUI(currentUser);
    }

    private void updateUI(FirebaseUser currentUser) {
    }

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

        mAuth = FirebaseAuth.getInstance();

        mAuth.addAuthStateListener(mAuthListner);

        mAuthListner = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    Intent intent = new Intent(Login.this, NavigationMenu.class);
                    startActivity(intent);
                    finish();
                }
            }
        };

        btnlogin=findViewById(R.id.btnLogin);
        mEmail=findViewById(R.id.edt_email);
        pass=findViewById(R.id.edt_pasword);
        btndaftar=findViewById(R.id.btnDaftar);

        btndaftar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent daftar = new Intent(Login.this, Daftar.class);
                startActivity(daftar);
            }
        });



        btnlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = mEmail.getText().toString();
                String password = pass.getText().toString();

                mAuth.signInWithEmailAndPassword(email, password)
                        .addOnCompleteListener(Login.this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if (task.isSuccessful()) {
                                    // Sign in success, update UI with the signed-in user's information
                                    Intent intent = new Intent(Login.this, NavigationMenu.class);
                                    startActivity(intent);
                                    finish();
                                    FirebaseUser currentUser = mAuth.getCurrentUser();
                                    updateUI(currentUser);
                                } else {
                                    // If sign in fails, display a message to the user.
                                    Toast.makeText(Login.this, "Login gagal !",
                                            Toast.LENGTH_SHORT).show();
                                    updateUI(null);
                                }
                            }
                        });
           }
        });
    }
}

打开应用后,它立即崩溃了。知道为什么会这样吗?

2 个答案:

答案 0 :(得分:0)

addAuthStateListener()的{​​{1}}方法中添加侦听器时,它还没有值,因此您的应用程序崩溃了

初始化onCreate(),然后再添加到mAuthListner

您需要像这样反转代码:

mAuth

答案 1 :(得分:0)

public class Login extends AppCompatActivity {

private FirebaseAuth mAuth;
FirebaseAuth.AuthStateListener mAuthListner;

Button btnlogin;
TextView btndaftar;
MaterialEditText mEmail, pass;

@Override
protected void onStart() {
    super.onStart();
    FirebaseUser currentUser = mAuth.getCurrentUser();
    updateUI(currentUser);

}

private void updateUI(FirebaseUser currentUser) {
}

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

    mAuth = FirebaseAuth.getInstance();

    mAuthListner = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                Intent intent = new Intent(Login.this, NavigationMenu.class);
                startActivity(intent);
                finish();
            }
        }
    };

    mAuth.addAuthStateListener(mAuthListner);

    btnlogin=findViewById(R.id.btnLogin);
    mEmail=findViewById(R.id.edt_email);
    pass=findViewById(R.id.edt_pasword);
    btndaftar=findViewById(R.id.btnDaftar);

    btndaftar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent daftar = new Intent(Login.this, Daftar.class);
            startActivity(daftar);
        }
    });



    btnlogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String email = mEmail.getText().toString();
            String password = pass.getText().toString();

            mAuth.signInWithEmailAndPassword(email, password)
                    .addOnCompleteListener(Login.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if (task.isSuccessful()) {
                                // Sign in success, update UI with the signed-in user's information
                                Intent intent = new Intent(Login.this, NavigationMenu.class);
                                startActivity(intent);
                                finish();
                                FirebaseUser currentUser = mAuth.getCurrentUser();
                                updateUI(currentUser);
                            } else {
                                // If sign in fails, display a message to the user.
                                Toast.makeText(Login.this, "Login gagal !",
                                        Toast.LENGTH_SHORT).show();
                                updateUI(null);
                            }
                        }
                    });
        }
    });
}

这是我换位置后的代码