Я пытаюсь сделать так, чтобы EditTextLayout выдавал сообщение об ошибке, когда они пусты, и приложение аварийно завершает работу.
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class LoginActivity extends AppCompatActivity {
private TextInputLayout textInputEmail , textInputPassword;
private Button login;
String emailInput , passwordInput;
private FirebaseAuth auth;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
auth = FirebaseAuth.getInstance();
textInputEmail = (TextInputLayout) findViewById(R.id.login_email);
textInputPassword = (TextInputLayout) findViewById(R.id.login_password);
login = (Button)findViewById(R.id.login_button);
progressDialog = new ProgressDialog(this);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ConfirmInput();
progressDialog.setTitle("Singing in");
progressDialog.setMessage("Please wait while we sign you in");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
auth.signInWithEmailAndPassword(emailInput , passwordInput).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
progressDialog.dismiss();
Toast.makeText(LoginActivity.this, "Account Created successfuly", Toast.LENGTH_SHORT).show();
SendUserToMainActivity();
}else{
progressDialog.dismiss();
Toast.makeText(LoginActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
private void SendUserToMainActivity() {
Intent intent = new Intent(LoginActivity.this , MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
public boolean ValidateEmail(){
emailInput = textInputEmail.getEditText().getText().toString().trim();
if (emailInput.isEmpty()){
textInputEmail.setError("You Can't leave this Empty!");
return false;
}else{
textInputEmail.setError(null);
return true;
}
}
public boolean ValidatePassword(){
passwordInput = textInputPassword.getEditText().getText().toString();
if (passwordInput.isEmpty()){
textInputPassword.setError("You Can't leave this Empty!");
return false;
}else{
textInputPassword.setError(null);
return true;
}
}
public void ConfirmInput(){
if (!ValidateEmail() | !ValidatePassword()){
return;
}
}
}
java.lang.IllegalArgumentException: Given String is empty or null
at com.google.android.gms.common.internal.zzbq.zzgm(Unknown Source)
at com.google.firebase.auth.FirebaseAuth.signInWithEmailAndPassword(Unknown Source)
at com.example.bestever.snapchatclone.LoginActivity$1.onClick(LoginActivity.java:50)
at android.view.View.performClick(View.java:5184)
at android.view.View$PerformClick.run(View.java:20893)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5938)
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:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Я также попытался изменить TextInputLayout на TextInputEditText, и ничего не изменилось. Я также попытался изменить свой подход к проверке того, пуст ли TextInputLayout, но он не работает.




Вы должны проверить, является ли textInputEmail.getEditText().getText() значением null, прежде чем извлекать из него текст, это может быть проблемой. Надеюсь, что это работает.
Вы не можете вызвать функцию getText() для нулевой строки. Пожалуйста, используйте этот метод, чтобы реализовать его правильно:
String emailInput;
emailInput = textInputEmail.getEditText().getText();
if (emailInput != null) {
emailInput = emailInput.toString().trim();
}
Или (Мой любимый способ)
String emailInput = "";
try {
emailInput = textInputEmail.getEditText().getText().toString().trim();
} catch (IllegalArgumentException ex) { }