当前位置: 动力学知识库 > 问答 > 编程问答 >

Edittext string verification issue

问题描述:

How can i verify that the EditText has a value (ie. it isn't empty or not null). I wrote the following code assigning the string value typed into an EditText to a String object but verifying that it isn't empty was an issue for me. Anytime i run it,i get a NullPointerException.

public class LoginFragment extends SherlockFragment {

EditText e1;

EditText e2;

Button b;

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInatanceState) {

View view = inflater.inflate(R.layout.login_layout, container, false);

e1 = (EditText) view.findViewById(R.id.reg_email);

e2 = (EditText) view.findViewById(R.id.reg_password);

b = (Button) view.findViewById(R.id.btnLogin);

b.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

String mEmail = e1.getText().toString(); // NullPointerException

String pwrd = e2.getText().toString();

if ((mEmail.equals(null)) || (pwrd.equals(null))) {

AlertDialog.Builder build = new AlertDialog.Builder(

getActivity());

build.setMessage("Please enter your login details");

build.setCancelable(false);

build.setPositiveButton("Ok",

new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog,

int which) {

dialog.cancel();

}

});

AlertDialog alert = build.create();

alert.show();

} else {

Toast.makeText(getActivity(), "Welcome", Toast.LENGTH_SHORT)

.show();

Intent intent = new Intent(getActivity(), Home.class);

startActivity(intent);

}

}

});

return view;

}

}

Can somebody help me with this? I read a similar question on this asked by someone else but it didn't solve my problem.

网友答案:

You dnt need to take value of that in string. Solution to this is as below. Definr this at class level

EditText username;
EditText password;

and then follow this

username = (EditText) findViewById(R.id.User_name_value);
password = (EditText) findViewById(R.id.password_value);
if (username.getText().toString().equals("") || password.getText().toString().equals("")) 
{
    // Log.i("TAG!","Inside if condition");
    Toast.makeText(this, user_text, Toast.LENGTH_SHORT).show();
} else {

}
分享给朋友:
您可能感兴趣的文章:
随机阅读: