簡體   English   中英

如何在Firebase身份驗證中修復空指針異常?

[英]How can i fix null pointer exception in 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);
                                }
                            }
                        });
           }
        });
    }
}

應用程序打開后,它剛剛崩潰。 知道為什么會這樣嗎?

當您將監聽器添加到onCreate()addAuthStateListener()方法時,它還沒有值,因此您的應用程序崩潰

初始化mAuthListner加入之前mAuth

你需要像這樣反轉你的代碼:

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

這是切換位置后的代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM