当用户登录并且他关闭移动数据或wi-fi我的应用程序崩溃。如果用户仍然连接到Internet或以某种方式处理此错误,如何在进程运行时检查。 提前谢谢你,我很抱歉我的英语。
firebaseAuth.signInWithEmailAndPassword(UserEmail, UserPassword)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
progressDialog.dismiss();
startActivity(new Intent(LogIn.this, MainActivity.class));
Toast.makeText(LogIn.this, "Logged in succesfully!", Toast.LENGTH_SHORT).show();
finish();
} else {
FirebaseAuthException e = (FirebaseAuthException) task.getException();
progressDialog.dismiss();
Toast.makeText(LogIn.this, "LogIn failed: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(LogIn.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
更新
java.lang.ClassCastException: com.google.firebase.FirebaseNetworkException cannot be cast to com.google.firebase.auth.FirebaseAuthException
at com.example.mypc.ex.LogInRegister.LogIn$2.onComplete(LogIn.java:104)
at com.google.android.gms.tasks.zzf.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
更新v2
firebaseAuth.signInWithEmailAndPassword(UserEmail, UserPassword)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
progressDialog.dismiss();
startActivity(new Intent(LogIn.this, MainActivity.class));
Toast.makeText(LogIn.this, "Logged in succesfully!", Toast.LENGTH_SHORT).show();
finish();
} else {
FirebaseException e = (FirebaseException) task.getException();
progressDialog.dismiss();
Toast.makeText(LogIn.this, "LogIn failed: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
});
答案 0 :(得分:0)
发布示例代码有助于澄清您的问题,但一般来说,您总是可以捕获非运行时异常并处理它。
所以你的代码部分执行与firebase的连接,用try catch块包装它。在日志中打印出错误消息,然后找出一个让用户知道的逻辑取决于该异常中抛出的消息。
编辑: 我在更新你的帖子后现在看到了问题:
else {
FirebaseAuthException e = (FirebaseAuthException) task.getException();
progressDialog.dismiss();
Toast.makeText(LogIn.this, "LogIn failed: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
您假设异常始终为FirebaseAuthException
,您可以在日志中看到它是FirebaseNetworkException
这是应该如何
firebaseAuth.signInWithEmailAndPassword(UserEmail, UserPassword)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
try{
if (task.isSuccessful()) {
progressDialog.dismiss();
startActivity(new Intent(LogIn.this, MainActivity.class));
Toast.makeText(LogIn.this, "Logged in succesfully!", Toast.LENGTH_SHORT)
.show();
finish();
}
}
catch (FirebaseAuthException | FirebaseNetworkException e){
progressDialog.dismiss();
Toast.makeText(LogIn.this, "LogIn failed: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(LogIn.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}