0

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();
Pjrat111
  • 213
  • 1
  • 3
  • 9
  • 1
    You parse `billAmount` and `tipPercent` to String and then to Double. What are they in the beginning? I see a lot of potential sources for the NumberFormatException. You could also insert log messages between each line to see where it breaks. – nuala Apr 22 '12 at 01:28
  • billAmount and tipPercent are editTexts – Pjrat111 Apr 22 '12 at 01:30

2 Answers2

5

Use billAmount.getText().toString(); instead of billAmount.toString();

and the same for tipPercent.

Hesham Saeed
  • 5,358
  • 7
  • 37
  • 57
  • Thank you, I was just editing my post to include that I had figured that out. Thanks for the post anyways, I wouldn't have remembered to do that if I hadn't seen a forum post on it. – Pjrat111 Apr 22 '12 at 01:39
  • Yes it happens, glad you figured it out, good luck in your coding! – Hesham Saeed Apr 22 '12 at 01:47
0

You need to check that the EditText fields have legal double values. The exception is coming from trying to parse something that does not look like a double.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521