i have currently started to learn how to create apps for android phones using java and it been great i tell you.i used to develop for desktops and now i'm laying my hands on mobile as part time.i am creating an app in android which has an edit text some kind of a text field and i'm trying to validate the text string inputted by the user to ensure that a string text is inputted.but the problem is the app crashes when i press a button e.g log in button which check for validation and when i check log cat,there's this NullPointerException it's throwing at the particular line i commented in this code.please can someone with better eyes look at the codes for me and point out my mistakes.i'd appreciate it
public class LoginFragmentActivity extends SherlockFragment {
// creating text fields with a log in button
EditText e1 = null;
EditText e2 = null;
Button b;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInatanceState) {
View view = inflater.inflate(R.layout.login_layout, container, false);
// instantiating each text field and button
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) {
// assigning the text obtained form the text fields to a string variable
String email = e1.getText().toString(); // NullPointerException is raised at this line
String pname = e2.getText().toString();
// validating inputs.
if ((email.equals("")) || (pname.equals(""))) {
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity());
builder.setMessage("Please fill in all fields");
builder.setCancelable(false);
builder.setPositiveButton("Ok",
new DialogInterface.onclickListener() {
@Override
public void onclick(DialogInterface dialog,
int which) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
} else {
Toast.makeText(getActivity(), "Welcome",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), Home.class);
startActivity(intent);
}
}
});
return view;
}
}
The funny thing is that this is the same line of codes i wrote for another activity and it's perfectly working with no errors.i'm confused!!.

New Topic/Question
Reply


MultiQuote






|