每当我尝试使用firebase auth注册用户时,我的应用就会崩溃。我的代码如下:
public class MainActivity extends AppCompatActivity {
private EditText mEmailField;
private EditText mPasswordField;
private Button mLoginBtn;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
mEmailField = (EditText)findViewById(R.id.emailField);
mPasswordField = (EditText)findViewById(R.id.passwordField);
mLoginBtn = (Button)findViewById(R.id.buttonLogin);
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser() != null) {
startActivity(new Intent(MainActivity.this, Home.class));
}
}
};
mLoginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startSignIn();
}
});
}
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
private void startSignIn() {
String email = mEmailField.getText().toString();
String password = mPasswordField.getText().toString();
if(TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {
Toast.makeText(MainActivity.this, "Fields are empty", Toast.LENGTH_LONG).show();
} else {
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Sign In Failed!!", Toast.LENGTH_LONG).show();
}
}
});
}
}
}
这是我的logcat:
08-03 05:50:41.766 17746-17746 / com.finalyearproject.vimelraj.qfoody E / Android运行时:致命异常:主要 流程:com.finalyearproject.vimelraj.qfoody,PID:17746 java.lang.NullPointerException:尝试调用虚拟方法'com.google.android.gms.tasks.Task com.google.android.gms.common.api.GoogleApi.zzb(com.google.android.gms.common.api .internal.zzdd)'引用为空对象 在com.google.android.gms.internal.zzdvv.zzb(未知来源) 位于com.google.android.gms.internal.zzdwc.zzb(未知来源) 在com.google.firebase.auth.FirebaseAuth.signInWithEmailAndPassword处(未知来源) 在com.finalyearproject.vimelraj.qfoody.MainActivity.startSignIn(MainActivity.java:91) 在com.finalyearproject.vimelraj.qfoody.MainActivity.access $ 000(MainActivity.java:23) 在com.finalyearproject.vimelraj.qfoody.MainActivity $ 2.onClick(MainActivity.java:64) 在android.view.View.performClick(View.java:5637) 在android.view.View $ PerformClick.run(View.java:22429) 在android.os.Handler.handleCallback(Handler.java:751) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6119) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:1)
在 onComplete 方法中使用以下代码。
new android.os.Handler().postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Sign In Failed!!", Toast.LENGTH_LONG).show();
}
}, 100);
更新
private void startSignIn() {
String email = mEmailField.getText().toString();
String password = mPasswordField.getText().toString();
if( email == null || password == null || TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {
Toast.makeText(MainActivity.this, "Fields are empty", Toast.LENGTH_LONG).show();
} else {
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
new android.os.Handler().postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Sign In Failed!!", Toast.LENGTH_LONG).show();
}
}, 100);
}
}
});
}
}
答案 1 :(得分:0)
以这种方式在UI线程上显示Toast
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "Sign In Failed!!", Toast.LENGTH_LONG).show();
}
});