单击TextInputLayout为空且按钮时,应用程序崩溃

问题描述 投票:0回答:2

我试图让EditText Layout在它们为空并且应用程序崩溃时发出错误消息

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是否为空,但它不起作用

java android firebase
2个回答
0
投票

你应该在从它检索文本之前验证textInputEmail.getEditText().getText()是否为null,这可能是问题所在。希望它有效。


0
投票

你不能在null String上调用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) { }
© www.soinside.com 2019 - 2024. All rights reserved.