每当我尝试通过类型转换为“FirebaseAuthException”从
task.getException()
收到的异常中获取错误代码时,它会给出 java.lang.ClassCastException: com.google.firebase.FirebaseException cannot be cast to com.google.firebase.auth.FirebaseAuthException
的错误,并且它总是返回
getMessage 方法出现“错误 [INVALID_LOGIN_CREDENTIALS]”
task.getException().getMessage();
这是导致错误的代码。
String errorCode = ((FirebaseAuthException) task.getException()).getErrorCode();
我提供了无效的凭据只是为了获得异常,但我没有得到确切的异常,例如密码错误、电子邮件不正确或找不到用户等。
我正在使用 Android Studio Giraffe,2022.3.1 补丁 2
这是我正在使用的依赖项
implementation("com.google.firebase:firebase-auth:22.1.2")
implementation("com.google.firebase:firebase-database:20.2.2")
implementation("com.google.firebase:firebase-storage:20.2.1")
当您使用以下代码行时:
String errorCode = ((FirebaseAuthException) task.getException()).getErrorCode();
task.getException()
返回的对象类型是FirebaseException。尝试将这样的对象转换为 类型的对象
FirebaseAuthException 确实会产生以下错误:
java.lang.ClassCastException:com.google.firebase.FirebaseException 无法转换为 com.google.firebase.auth.FirebaseAuthException
为什么?因为你不能将超类的对象转换为子类的对象。请注意,继承关系是 FirebaseAuthException 扩展 FirebaseException,反之亦然。其中 FirebaseException 类是超类,而 FirebaseAuthException 是子类。
另请注意,仅当在 Firebase 控制台中禁用了通过 startActivityForSignInWithProvider 方法登录,或者传递的提供程序配置不正确时,才会引发 FirebaseAuthException。
如果您想获取有关异常的更多详细信息,那么您应该考虑调用 getCause()、getMessage() 或 printStackTrace()。