4 回答
TA贡献1886条经验 获得超2个赞
我认为错误非常明显。调用getMessage()可能为 null,因为task.getException()可能返回 null。
在调用之前,您应该确保任务异常不为空getMessage():
if (task.isSuccessful()) {
...
} else {
Throwable taskException = task.getException();
// Before invoking getMessage() on the exception,
// we make sure the exception is not null
if (taskException != null) {
Toast.makeText(Login.this, taskException.getMessage(), Toast.LENGTH_LONG).show();
}
}
为了您的信息,您可能还应该在制作和显示吐司之前检查异常消息是否不为空。
TA贡献1765条经验 获得超5个赞
您必须对方法进行一些更改:-
这是检查edittext是否为空:-
public Boolean checkEmpty(EditText edittext) {
if (edittext.getText().toString().isEmpty()){
return true;
}else{
return false;
}
}
这是在您的 onCreate 方法中:-
if(checkEmpty(etEmail)) {
Toast.makeText(StartActivity.this, "Email is empty", Toast.LENGTH_SHORT).show();
}else if(checkEmpty(etPass)){
Toast.makeText(StartActivity.this, "password is empty", Toast.LENGTH_SHORT).show();
}else {
firebaseAuth.signInWithEmailAndPassword(
etEmail.getText().toString(),
etPass.getText().toString())
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
//this starts activity if able to sign in
if (task.isSuccessful()) {
Toast.makeText(Login.this, "Login Successful",
Toast.LENGTH_LONG).show();
startActivity(new Intent(Login.this,
MainActivity.class));
} else {
//if task unsuccessful it should show firebase toast messages
Toast.makeText(Login.this, task.getException().getMessage(),
Toast.LENGTH_LONG).show();
//this getMessage does not work due to error above
}
}
});
}
TA贡献1817条经验 获得超14个赞
我已经改进了我的代码,我非常感谢所有帮助我理解更多的答案。我已经完全摆脱了该checkEmpty()方法,并选择在我的 onClickListener 中定义每个 editText。
passStr = etPass.getText().toString();
emailStr = etEmail.getText().toString();
然后在 if 和 else if 语句中使用
if (**emailStr**.isEmpty())
else if (**passStr**.isEmpty())
带有“请输入密码”和“请输入电子邮件地址”的吐司消息。
注册完成后(它使用 a.addOnCompleteListener(task)来检查firebaseAuth.signInWithEmailAndPassword()),它会执行以下操作:
if (task.isSuccessful()) {
Toast.makeText(LoginActivity.this, "Login Successful", Toast.LENGTH_LONG).show();
startActivity(new Intent(LoginActivity.this, Home.class));
} else {
Throwable taskException = task.getException();
if (taskException != null) {
Toast.makeText(LoginActivity.this, task.getException().getMessage(),
Toast.LENGTH_LONG).show();
}
}
因此,它在显示 firebase toast 消息之前首先检查输入字段是否为空。我意识到我使用 checkEmpty 方法过于复杂了,因为我需要做的就是将每一个定义为 onClickListener 中的字符串。我不需要编写新方法,因为我没有在其他任何地方使用它。
添加回答
举报
