0

I am new to Android, and am having a baffling problem. I am trying to perform some numeric calculations on some text entered in one or more EditText fields (every field has a corresponding algorithm to determine a result), and then store the result/results in some TextViews associated with the EditTexts. EditText1 is associated with TextView1; EditText2 is associated with TextView2. In my program, there will be ten EditTexts, each of which has an associated TextView. All or none of the EditTexts may have some numeric data in them. The calculation takes place after a button is pressed (using andriod:onClick="buttonPressed"), at which point any EditTexts that have numeric input should have their data pulled out and calculated upon (with a formula specific to that piece of data), with the result being stored in the EditText's particular TextView.

When I only use one EditText and one TextView, everything is fine. The data from the EditText is properly parsed, calculated upon, and the result is stored in the TextView. Here is the code in that case:

public void buttonPressed(View view)
{
    if (editText1.getText() != null)
        textView1.setText(Double.toString(Double.valueOf(editText1.getText().toString())/12));
}   

However, if I try to extend this code and use another EditText and TextView like this:

public void buttonPressed(View view)
{
    if (editText1.getText() != null)
        textView1.setText(Double.toString(Double.valueOf(editText1.getText().toString())/12));
    if (editText2.getText() != null)
        textView2.setText(Double.toString(Double.valueOf(editText2.getText().toString())/107));
}   

I get some errors that seem to indicate a parsing problem. Here is the LogCat output (I am not great at interpreting this):

11-07 20:38:14.191: E/AndroidRuntime(1570): FATAL EXCEPTION: main
11-07 20:38:14.191: E/AndroidRuntime(1570): java.lang.IllegalStateException: Could not execute method of  the activity
11-07 20:38:14.191: E/AndroidRuntime(1570):     at android.view.View$1.onClick(View.java:3597)
11-07 20:38:14.191: E/AndroidRuntime(1570):     at android.view.View.performClick(View.java:4202)
11-07 20:38:14.191: E/AndroidRuntime(1570):     at android.view.View$PerformClick.run(View.java:17340)
11-07 20:38:14.191: E/AndroidRuntime(1570):     at android.os.Handler.handleCallback(Handler.java:725)
11-07 20:38:14.191: E/AndroidRuntime(1570):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-07 20:38:14.191: E/AndroidRuntime(1570):     at android.os.Looper.loop(Looper.java:137)
11-07 20:38:14.191: E/AndroidRuntime(1570):     at android.app.ActivityThread.main(ActivityThread.java:5039)
11-07 20:38:14.191: E/AndroidRuntime(1570):     at java.lang.reflect.Method.invokeNative(Native Method)
11-07 20:38:14.191: E/AndroidRuntime(1570):     at java.lang.reflect.Method.invoke(Method.java:511)
11-07 20:38:14.191: E/AndroidRuntime(1570):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-07 20:38:14.191: E/AndroidRuntime(1570):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-07 20:38:14.191: E/AndroidRuntime(1570):     at dalvik.system.NativeStart.main(Native Method)
11-07 20:38:14.191: E/AndroidRuntime(1570): Caused by: java.lang.reflect.InvocationTargetException
11-07 20:38:14.191: E/AndroidRuntime(1570):     at java.lang.reflect.Method.invokeNative(Native Method)
11-07 20:38:14.191: E/AndroidRuntime(1570):     at java.lang.reflect.Method.invoke(Method.java:511)
11-07 20:38:14.191: E/AndroidRuntime(1570):     at android.view.View$1.onClick(View.java:3592)
11-07 20:38:14.191: E/AndroidRuntime(1570):     ... 11 more
11-07 20:38:14.191: E/AndroidRuntime(1570): Caused by: java.lang.NumberFormatException: Invalid double: ""
11-07 20:38:14.191: E/AndroidRuntime(1570):     at java.lang.StringToReal.invalidReal(StringToReal.java:63)
11-07 20:38:14.191: E/AndroidRuntime(1570):     at java.lang.StringToReal.parseDouble(StringToReal.java:248)
11-07 20:38:14.191: E/AndroidRuntime(1570):     at java.lang.Double.parseDouble(Double.java:295)
11-07 20:38:14.191: E/AndroidRuntime(1570):     at java.lang.Double.valueOf(Double.java:332)
11-07 20:38:14.191: E/AndroidRuntime(1570):     at com.example.breadmakingconversions.MainActivity.sendToConvert(MainActivity.java:160)
11-07 20:38:14.191: E/AndroidRuntime(1570):     ... 14 more

I do not understand why the parsing method I have used to perform calculations on this data in the EditText (I was not sure how to do this, but I found this way by a lot of trial and error) should work for one field but not two. I have tried many alternate ways of doing this over the past few hours and cannot figure it out.

Any help greatly appreciated!

Edit: I forgot to say that all my EditTexts have android:numeric="decimal" (I know it's deprecated, but I am using it here anyway) so that I will never have any input that is not numeric.

Kallaste
  • 737
  • 1
  • 9
  • 18

2 Answers2

1

You just need to have an if clause around the conversion to make sure the string isn't empty.

The failure is when you are trying to convert an empty string to a double.

Change this line:

if(editText1.getText() != null){

To:

if(editText1.getText() != null && !TextUtils.isEmpty(editText1.getText().toString().trim())){

Now if a user inputs a non number in the EditText, you are still going to have problems.

Put a try/catch statement around the whole mess, to catch NumberFormatExceptions.

I just add this statement because sof does not allow one character edition if statement needs another !(not) also

Ismail Sahin
  • 2,640
  • 5
  • 31
  • 58
Jon F Hancock
  • 3,349
  • 3
  • 23
  • 26
0

One of your fields contains an empty string, instead of null. You need to expand the conditions like this ...

if (editText1.getText() != null)

to be something more like

if (editText1.getText() != null && !editText1.getText().toString().equals(""))

to catch the empty string case. There are also a couple of shorter ways of writing this. You might like to look into Apache's StringUtils class, which has methods called isEmpty and isBlank which may be of some use to you.

You probably also want to think about what to do if other invalid values find their way into one of those fields.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • 1
    Your method doesn't take into account spaces. You should trim the string before checking if it is empty. Also Android has TextUtils built in to do the same job as StringUtils in that case. – Jon F Hancock Nov 07 '13 at 21:11
  • The "spaces" issue was why I mentioned `isBlank`. I would never write all of this with `trim` - I always use one of the `StringUtils` methods. Thank you for the note about `TextUtils` though; I didn't know that Android had that. – Dawood ibn Kareem Nov 07 '13 at 21:18
  • Thank you! I wish I could accept both answers, since they are both correct and I learned something from both. However, my error stemmed from the fact that I incorrectly believed that null and "" were the same thing (so I was not testing for ""), but from your answer I learned that "" is the empty string, which is something different. Thanks again to everyone who helped! – Kallaste Nov 07 '13 at 22:16
  • You may learn something from the answers to these two questions - http://stackoverflow.com/questions/4802015/difference-between-null-and-java-string http://stackoverflow.com/questions/2601978/how-to-check-if-my-string-is-equal-to-null - in which the people posting were confused about the distinction between empty and null. It's a fairly common topic here on Stack Overflow. – Dawood ibn Kareem Nov 07 '13 at 22:40