java - 如何修复 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);
}
}
});
}
});
}
}
应用程序打开后,它就崩溃了。知道为什么会这样吗?
解决方案
当您将侦听器添加到其中的addAuthStateListener()
方法时,onCreate()
它还没有值,因此您的应用程序崩溃
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);
推荐阅读
- java - 为什么 List.contains(Object) 的行为不同?
- r - 如何在同一个数据帧上做colsum和average
- powerbi - 使用 DAX 生成日期系列
- javascript - 仅在服务器上需要一个包
- java - Intellij IDEA 仅针对所有未提交的更改运行测试
- regex - 在换行符之前匹配字符,不包括空格?
- macos - MacOS - 使用带有launchd的脚本 - 启动,登录,注销,关闭?
- service-worker - Workbox 的服务人员在更改时未更新
- python - AllenNLP 共指分辨率的多 GPU 训练
- reactjs - 事件处理程序中带有 [name] 的 PrevState