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.