Stack Overflow archive
0 score

Calculate button is not working in android

score
0
question views
218
license
CC BY-SA 3.0

The problem:

One (or all) of your EditTexts is not a valid integer and is failing on Integer.parseInt(String). You should handle any NumberFormatException and check the value of your EditTexts (feet, inches, lbs).


From your logcat:

12-17 03:11:54.910: E/AndroidRuntime(872): java.lang.NumberFormatException: Invalid int: ""


Proposed fix:

You could add the following method to your Activity class:

java
private int getIntFromEditText(EditText editText, int defaultValue) {
    try {
        return Integer.parseInt(editText.getText().toString().trim());
    } catch (Exception e) {
        return defaultValue;
    }
}

and then change

java
hfeet = Integer.parseInt(feet.getText().toString());
hinc = Integer.parseInt(inches.getText().toString());
wlbs = Integer.parseInt(lbs.getText().toString());

to

java
hfeet = getIntFromEditText(feet, 0);
hinc = getIntFromEditText(inches, 0);
wlbs = getIntFromEditText(lbs, 0);

Originally posted on Stack Overflow. Public user contributions are licensed under Creative Commons Attribution-ShareAlike.