I have two EditText fields in an app. The first EditText is set up to accept strings, in my case a name:
EditText nameField = (EditText) findViewById(R.id.nameOfCustomer);
String name = nameField.getText().toString();
I have two EditText fields in an app. The first EditText is set up to accept strings, in my case a name:
EditText nameField = (EditText) findViewById(R.id.nameOfCustomer);
String name = nameField.getText().toString();
I then added the following java code for the first EditText to check for nulls:
if(TextUtils.isEmpty(name))
{
Toast.makeText(this, "Please Enter Name ",Toast.LENGTH_LONG).show();
return;
}
This works just fine and when the TextUtils.isEmpty is true it causes the toast to display if no entry is made in the first EditText field.
The second EditText has the input type set to android:inputType="number" since I want to have the user enter a number only
I have duplicated this same code for the second EditText:
EditText nameField2 = (EditText) findViewById(R.id.ageOfPatient);
String name2 = nameField2.getText().toString();
and I check it for nulls before I parse to an integer:
if(TextUtils.isEmpty(name2)){
Toast.makeText(this, "Please Enter Patient Age ",Toast.LENGTH_SHORT).show();
return;
}
int val = Integer.parseInt( nameField2.getText().toString() );
However, it does not identify a null condition and causes the application to crash. I could not find anything that works.
Any suggestions would be most apprecited !
I then added the following java code for the first EditText to check for nulls:
if(TextUtils.isEmpty(name)) {
Toast.makeText(this, "Please Enter Name ",Toast.LENGTH_LONG).show();
return;
}
This works just fine and when the TextUtils.isEmpty is true it causes the toast to display if no entry is made in the first EditText field.
The second EditText has the input type set to android:inputType="number" since I want to have the user enter a number only
I have duplicated this same code for the second EditText:
EditText nameField2 = (EditText) findViewById(R.id.ageOfPatient);
String name2 = nameField2.getText().toString();
and I check it for nulls before I parse to an integer:
if(TextUtils.isEmpty(name2)){
Toast.makeText(this, "Please Enter Patient Age ",Toast.LENGTH_SHORT).show();
return;
}
int val = Integer.parseInt( nameField2.getText().toString() );
However, it does not identify a null condition and causes the application to crash. I could not find anything that works. Any suggestions would be most apprecited !
I discovered that I had android:text for the second EditText instead of Android:hint so the null checker was seeing text instead of what I thought was the hint, duh. I stumbled across this when I deleted the text in test mode and found the code worked just fine, so I went back to compare the code on both EditText fields and sure enought, the second text had android:text instead of android:hint. Thanks to all who helped me out. As I stated, I am a novice, but quickly learning. Thanks again to all ! Tom