Search code examples
javaandroidnumberformatexception

My app keeps crashing after I click a button


I have been programming an app on my own, but it is running into some problems everytime I click one button (since the problem is in its OnCreate method). I have narrowed it down to what I think is the cause and it seems to be this line:

int desitky_tom = Integer.parseInt(et_tom.getText().toString());

I want to change the et_tom variable from String which I receive from an editText to an int. Could this be the cause? If yes, how do i change it do the app does not crash anymore?

This is my Logcat Error log:

2019-02-07 16:51:16.272 2794-2794/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mariaspocitadlo, PID: 2794
java.lang.NumberFormatException: For input string: ""
    at java.lang.Integer.parseInt(Integer.java:620)
    at java.lang.Integer.parseInt(Integer.java:643)
    at com.example.mariaspocitadlo.MainActivity$1.onClick(MainActivity.java:41)
    at android.view.View.performClick(View.java:6897)
    at android.widget.TextView.performClick(TextView.java:12693)
    at android.view.View$PerformClick.run(View.java:26101)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6944)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

And here is the rest of the code:

final Button spocitat = (Button) findViewById(R.id.button);
    spocitat.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //editext Toma na desítky Toma
            EditText et_tom = (EditText)findViewById(R.id.editText);
            int desitky_tom = Integer.parseInt(et_tom.getText().toString());

            TextView txtview = (TextView)findViewById(R.id.textView_tom);
            String str = Double.toString(desitky_tom);
            txtview.setText(str);
            //editext Táty na desítky Táty
            EditText et_tat = (EditText)findViewById(R.id.editText2);
            int desitky_tat = Integer.parseInt(et_tat.getText().toString());
            //editext Kuby na desítky Kuby
            EditText et_kub = (EditText)findViewById(R.id.editText3);
            int desitky_kub = Integer.parseInt(et_kub.getText().toString());

            int desitky_vitez = 0;
            int body_tom = 0;
            int body_tat = 0;
            int body_kub = 0;
            int body_viteze = 0;

Solution

  • Judging from your LogCat the entered String is "". (line 3 in your LogCat). Therefore its not a number and cannot be converted to int. You should make sure you input a number, to stop the crashes surround your code with try and catch like:

    try {
     EditText et_tom = (EditText)findViewById(R.id.editText);
     int desitky_tom = Integer.parseInt(et_tom.getText().toString());
    } catch (Exception e) { System.out.println(e.toString()); }
    

    Other than that loop through the input and make sure the entered text is only from an array of numbers.