如何修复 Firebase 身份验证中的空指针异常



我需要这方面的帮助。每次我运行该应用程序时,每次在登录活动时打开它都会崩溃。请帮忙,我被这个困住了。我已经遵循了很多教程,我认为代码是相同的,但仍然出现空指针异常。我不知道如何修复这个空指针。

日志猫 :

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);
                                }
                            }
                        });
           }
        });
    }
}

应用程序打开后,它刚刚崩溃。 知道为什么会这样吗?

当您在 onCreate() 中将侦听器添加到 addAuthStateListener() 方法时,它还没有值,因此您的应用程序崩溃

在添加到mAuth之前初始化mAuthListner

您需要像这样反转代码:

   // First create your listener instance
   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();
                }
            }
        };
   // And so, add it to the addAuthStateListener() method
   mAuth.addAuthStateListener(mAuthListner);
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);
                            }
                        }
                    });
        }
    });
}

这是我切换位置后的代码

最新更新