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" />
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 StringIn 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:
Edit your layout file
bez you are passing empty String in parseInt() so check before parsing string to int as:
Your
EditText
initially has an emptyString
for its value (ie""
). This is obviously not a number, so when you try to callint 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...
Or set the initial value as
"0"
for theEditText
, however this also displays the value0
in theEditText
on your interface rather than being empty, so it might not be suitable for all purposes...If you want to detect the error and handle it properly, you would do this...
This basically sets the value of
i = 0
, and will only change it to a different value if thetry{}
code doesn't give an error.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....