EditText设置错误

时间:2017-02-18 17:21:27

标签: android android-edittext

如果用户在没有填写文本字段的情况下保持登录按钮,我会尝试在编辑文本上设置错误。我尝试了几个代码,但没有任何帮助。每次运行它时应用程序都会崩溃。

这是代码

    public class LoginFragment extends Fragment {

private ProgressDialog progressDialog;
private FirebaseAuth firebaseAuth;
EditText email,password;
Button login;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.activity_login_fragment, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    firebaseAuth = firebaseAuth.getInstance();

    progressDialog = new ProgressDialog(getActivity());

    login = (Button)getActivity().findViewById(R.id.loginBtn);
    email = (EditText)getActivity().findViewById(R.id.regEmailEt);
    password = (EditText)getActivity().findViewById(R.id.regPasswordEt);


    login.setOnClickListener(new View.OnClickListener() {
        String email1;
        String password1;
            @Override
            public void onClick(View v) {
                if(email.getText().length() == 0){
                    email.setError("Please enter your email");
                }
                if(password.getText().length() == 0){
                    password.setError("Please enter your password");
                }

                     email1 = email.getText().toString();
                     password1 = password.getText().toString();

                progressDialog.setMessage("Processing...");
                progressDialog.show();


                   FirebaseAuth.getInstance().createUserWithEmailAndPassword(email1, password1)
                           .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                               @Override
                               public void onComplete(Task<AuthResult> task) {
                                   if (task.isSuccessful()) {
                                       startActivity(new Intent(getActivity(), MainActivity.class));
                                       progressDialog.dismiss();
                                   }
                               }
                           }).addOnFailureListener(new OnFailureListener() {
                       @Override
                       public void onFailure(Exception e) {
                           Toast.makeText(getActivity(), "Error:" + e.getMessage(), Toast.LENGTH_LONG).show();
                           progressDialog.dismiss();
                       }
                   });
               }

        });

       }
   }

当按下登录按钮时,它应该激活setOnClickListener。 我想检查文本字段是否为空,如果它们是这样我想在textEdit中设置setError。否则,我希望它继续下去。 我很确定我的代码是错的,所以我需要你的建议。

崩溃日志

     02-18 19:32:46.012 11162-11162/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.world.bolandian.whereareyou, PID: 11162
                                               java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
                                                   at com.world.bolandian.whereareyou.LoginFragment$1.onClick(LoginFragment.java:52)
                                                   at android.view.View.performClick(View.java:5207)
                                                   at android.view.View$PerformClick.run(View.java:21177)
                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                   at android.os.Looper.loop(Looper.java:148)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5441)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)

第52行 第52行是 - &gt;如果(email.getText()。长度()== 0)

3 个答案:

答案 0 :(得分:0)

 String email1;
    String password1;
        @Override
        public void onClick(View v) {
            if(email.getText().length() == 0){
                email.setError("Please enter your email");
            }
            if(password.getText().length() == 0){
                password.setError("Please enter your password");
            }

我会实现一个.toString并将其放在一个单独的方法中,让你的onClick调用该方法。

我有工作,所以我知道这是可行的。

答案 1 :(得分:0)

你应该检查空状态,如:

if (editText.getText().toString().trim().length() = 0) {
   //editText is Empty
   editText.setError("Your message");
   return;   //add this so rest of the code will not execute
}

这是另一种方式:

 EditText etName = (EditText) findViewById(R.id.etname);
String strName = etName.getText().toString();

 if(TextUtils.isEmpty(strName)) {
    etName.setError("Your message");
    return;
 }

您还应将onCreatView()更改为:

 View view;

 @Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.activity_login_fragment, container, false);
    login = (Button)view.findViewById(R.id.loginBtn);
    email = (EditText)view.findViewById(R.id.regEmailEt);
    password = (EditText)view.findViewById(R.id.regPasswordEt);

    return view;
}

答案 2 :(得分:0)

@Override
public void onClick(View v) {
  String email, password;
    email = emailfield.getText().toString();
    password = passfield.getText().toString();

    if(!(email.isEmpty())) {
        if(!(password.isEmpty())) {

            //run your process here

        } else {
            passfield.setError("Password Field Empty");
        }
    } else {
        emailfield.setError("Email Empty");
    }
}