I'm trying to create a for
-loop to add views to a layout. The for
-loop is working fine (I tried it with initialized variables) but I need to get an integer from an EditText
. I used a try-catch
and LogCat tells me the following...
java.lang.NumberFormatException: invalid int "".
The website developers.android.com says this is from incorrectly converting a string to an integer, but I don't understand how I could be incorrectly getting the data from the EditText
.
This is my code...
public class UserPref2Activity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
try
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText numSensors = (EditText) findViewById(R.id.num_sensors);
String change = numSensors.getText().toString();
int i = Integer.parseInt(change);
int j; //iterates through the for loop
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
for(j=1;j<i;j++)
{
EditText name = new EditText(this);
name.setText("Name:");
EditText type = new EditText(this);
type.setText("Type:");
EditText bits = new EditText(this);
bits.setText("Bits:");
ll.addView(name);
ll.addView(type);
ll.addView(bits);
}
this.setContentView(sv);
}
catch (Exception e)
{
//sends actual error message to the log
Log.e("ERROR", "ERROR IN CODE:" + e.toString());
//prints out location of error
e.printStackTrace();
}
}
}
And this is my XML file...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/userLayout" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter number of sensors:" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:id="@+id/num_sensors" />
Your EditText
initially has an empty String
for its value (ie ""
). This is obviously not a number, so when you try to call int i = Integer.parseInt(change);
it is giving you an error saying as such.
There are a few ways to fix this, but it basically boils down to either prevent the error by setting an initial value, or detecting an empty string and handling it properly.
To prevent the error from occurring...
EditText numSensors = (EditText) findViewById(R.id.num_sensors);
String change = numSensors.getText().toString();
if (change.equals("")){ // detect an empty string and set it to "0" instead
change = "0";
}
int i = Integer.parseInt(change);
Or set the initial value as "0"
for the EditText
, however this also displays the value 0
in the EditText
on your interface rather than being empty, so it might not be suitable for all purposes...
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text="0"
android:id="@+id/num_sensors" />
If you want to detect the error and handle it properly, you would do this...
EditText numSensors = (EditText) findViewById(R.id.num_sensors);
String change = numSensors.getText().toString();
int i = 0; // set it to 0 as the default
try {
i = Integer.parseInt(change);
}
catch (NumberFormatException e){}
This basically sets the value of i = 0
, and will only change it to a different value if the try{}
code doesn't give an error.
If you try to parse empty string it will always throw NumberFormatException
.
Change your code to following.
int i;
String change = numSensors.getText().toString();
if(change.length>0)
{
i = Integer.parseInt(change);
}
else
{
i=0;
}
I think initial Edit text will be empty so "" (blank is not a valid input
) is thowing error you should fill the data in Edit text and then on click on any event you should do the parsing work....
Edit your layout file
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text="0"
android:id="@+id/num_sensors" />
You are getting this error because there is nothing or a ""
/Empty String initially in your edit text. It makes no sense to get the text of an edit text when it is still being display. You may probably want to have a button or some other event which will signify that the user is done with inputting the value in the edit text and then you can go ahead and get the value of the edit text. But always have a check for the Empty String
bez you are passing empty String in parseInt() so check before parsing string to int as:
EditText numSensors = (EditText) findViewById(R.id.num_sensors);
String change = numSensors.getText().toString();
if (change.trim().equals("")) {
//DO SOMETHING HERE
}
else
{
int i = Integer.parseInt(change);
YOUR CODE HERE....
}
In order to prevent an int from getting a "" value, which is what happens when there's a second click in an empty EditText field, you can throw this line in your java code:
int i;
String change = numSensors.getText().toString();
if(!(change.equals("")))
{
i = Integer.parseInt(change);
}