简体   繁体   中英

App crashes when TextInputLayout empty and button is clicked

I am trying to make the EditTextLayout makes an error message when they are empty and the app crashes

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)

I tried also to change The TextInputLayout to TextInputEditText and nothing changed. I also tried to change my approach for checking if the TextInputLayout is empty, but it doesn't work

You should validate if textInputEmail.getEditText().getText() is null before retrieving the text from it, that might be the problem. Hope it works.

You can't call getText() function on null String. Please use this method to implement it in the right way:

    String emailInput;
    emailInput = textInputEmail.getEditText().getText();

    if (emailInput != null) {
        emailInput = emailInput.toString().trim();
    }

Or (My favorite way)

    String emailInput = "";

    try {
        emailInput = textInputEmail.getEditText().getText().toString().trim();
    } catch (IllegalArgumentException ex) { }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM