Question

I have an android app which has a few edittext boxes. I need to convert one of the edittext box value to use for calculation. I have the following code:

final EditText taken = (EditText)findViewById(R.id.editText1);
String no=taken.getText().toString();
int no2=Integer.parseInt(no);

For some reason, everytime I run the app on my phone (JB 4.2.2), it FC but if i comment out the code my app works just fine. Any idea what am I missing?

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/textView1"
    android:layout_toRightOf="@+id/textView1"
    android:ems="10"
    android:inputType="number" >
    <requestFocus />
</EditText>
Was it helpful?

Solution

Please, post the output of the logcat. Probably, this happens when in your EditText you don't have a number (other symbol). You can try something like this:

        int no2;
        try {
            no2=Integer.parseInt(no);
        } catch (NumberFormatException nfe) {
            no2=0; //default value. Ya can trigger some event here
        }

OTHER TIPS

You need to use try...catch exception for handle errors.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top