简体   繁体   中英

Android application crashes when EditText is empty

I have a problem, that i can't solve, and solution of teacher doesn't work.

The problem is when I click "Check the answer" and nothing is written in EditText , the program crashes. I work in Android Studio.

Java code:

public class Main extends AppCompatActivity {
    int a, b, c, d;
    Button Enter, NewExercise, EndLesson;
    EditText et;
    TextView tv;
    ImageView iv;
    String st;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);
        et = (EditText) findViewById(R.id.et);
        iv = (ImageView) findViewById(R.id.iv);
        a = (int)(10 * Math.random());
        b = (int)(10 * Math.random());
        c = a + b;
        tv.setText("Exmaple: " + a + " + " + b);
        et.setText("" + c);
    }

    public void Show(View view) {
        a = (int)(10 * Math.random());
        b = (int)(10 * Math.random());
        c = a + b;
        tv.setText("" + a + " + " + b);
        et.setText("");
    }

    public void Check(View view) {
        st = et.getText().toString();
        d = Integer.parseInt(st);
        if(0 == et.length()) {
            Toast.makeText(this,"Please, enter the answer",Toast.LENGTH_LONG).show();
        }
        if (d == c) {
            iv.setImageResource(R.drawable.t);
            Toast.makeText(this, "Right", Toast.LENGTH_LONG).show();
        }
        else {
            Toast.makeText(this, "False", Toast.LENGTH_LONG).show();
            iv.setImageResource(R.drawable.f);
            et.setText("");
        }
    }
}

Xml:

<TextView
    android:layout_width="wrap_content"
    android:textSize="30dp"
    android:textColor="#000000"
    android:layout_height="wrap_content"
    android:id="@+id/tv" />

<ImageView
    android:layout_width="match_parent"
    android:layout_height="71dp"
    app:srcCompat="@drawable/eq"
    android:id="@+id/eq" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:textSize="30dp"
    android:hint="Enter the answer"
    android:ems="10"
    android:maxLength="3"
    android:gravity="center"
    android:id="@+id/et"
    android:layout_weight="0.13" />

<ImageView
    android:layout_width="match_parent"
    android:layout_height="48dp"
    app:srcCompat="@drawable/t"
    android:id="@+id/iv"
    android:layout_weight="0.12" />

<Button
    android:text= "Check the answer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="Check"
    android:id="@+id/CheckTheAnswer" />

<Button
    android:text= "New Exercise"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="Show"
    android:id="@+id/NewExercise" />

<Button
    android:text="End lesson"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="Exit"
    android:id="@+id/EndLesson" />

I'm sorry for my English. Thank you in advance.

That's because you are parsing empty string (ie "") to int which is causing crash.

You should carefully handle exception while parsing String to int using try-catch, because if String can't be parsed into int then it throw Runtime error, which will cause application to crash.

Make a check to the EditText if it is empty or not using this:

protected boolean isEmpty(EditText editText) {
        return (editText.getText().toString().equals(""));
    }

Hope this helps.

Try putting a null checker. Example:

if(et != null){
  st = et.getText().toString().trim();
       d = Integer.parseInt(st);
}

I added trim() method as well to trim trailing and leading white spaces.

Thanks for answers, but only this solution work.

if (st.equals(""))
    {
  vbsays.setText("< First, you need to enter something into the text box, and then check your answer. Try!");
    }
    else 
{
        d = Integer.parseInt(st);
}

the return type of getText() is editable so we need to convert it into string by using toString().

if(t1.getText().toString().equals("")// t1 is Edittext(class) object
   {
       Toast.makeText(MainActivity.this, "my Message", Toast.LENGTH_LONG).show();
   }
else
   {
       //do ur operation
   }  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM