I always have issues for some reason when trying to set a textviews text to a number. In the following code I am trying to make a tip calculator, but when I run the app it force closes. The app works perfectly fine when I remove the items in the OnClick listener. I'll post the logcat after the code. Please tell me anything else that is helpful that I can post to improve the chance of my question getting answered.
billAmount = (EditText) findViewById(R.id.billAmount);
tipPercent = (EditText) findViewById(R.id.tipPercent);
Those are the editTexts.
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.calculateTip:
double billAmt = Double.parseDouble(billAmount.toString());
double tipAmt = Double.parseDouble(tipPercent.toString());
double billtip = (billAmt * tipAmt) / 100;
totalTip.setText("" + Double.toString(billtip));
break;
}
}
Here's the logcat
04-22 01:20:20.104: W/KeyCharacterMap(589): No keyboard for id 0
04-22 01:20:20.104: W/KeyCharacterMap(589): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
04-22 01:20:24.174: D/AndroidRuntime(589): Shutting down VM
04-22 01:20:24.174: W/dalvikvm(589): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-22 01:20:24.195: E/AndroidRuntime(589): FATAL EXCEPTION: main
04-22 01:20:24.195: E/AndroidRuntime(589): java.lang.NumberFormatException: android.widget.EditText@44f54a78
04-22 01:20:24.195: E/AndroidRuntime(589): at org.apache.harmony.luni.util.FloatingPointParser.initialParse(FloatingPointParser.java:130)
04-22 01:20:24.195: E/AndroidRuntime(589): at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:281)
04-22 01:20:24.195: E/AndroidRuntime(589): at java.lang.Double.parseDouble(Double.java:287)
04-22 01:20:24.195: E/AndroidRuntime(589): at com.tool.TipCalculator.onClick(TipCalculator.java:38)
04-22 01:20:24.195: E/AndroidRuntime(589): at android.view.View.performClick(View.java:2408)
04-22 01:20:24.195: E/AndroidRuntime(589): at android.view.View$PerformClick.run(View.java:8816)
04-22 01:20:24.195: E/AndroidRuntime(589): at android.os.Handler.handleCallback(Handler.java:587)
04-22 01:20:24.195: E/AndroidRuntime(589): at android.os.Handler.dispatchMessage(Handler.java:92)
04-22 01:20:24.195: E/AndroidRuntime(589): at android.os.Looper.loop(Looper.java:123)
04-22 01:20:24.195: E/AndroidRuntime(589): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-22 01:20:24.195: E/AndroidRuntime(589): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 01:20:24.195: E/AndroidRuntime(589): at java.lang.reflect.Method.invoke(Method.java:521)
04-22 01:20:24.195: E/AndroidRuntime(589): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-22 01:20:24.195: E/AndroidRuntime(589): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-22 01:20:24.195: E/AndroidRuntime(589): at dalvik.system.NativeStart.main(Native Method)
I'm fairly sure I have solved the problem. just doing
Double.parseDouble(editText.toString();
Doesn't work. I needed to add in
Double.parseDouble(editText.getText().toString();