Android Java NumberFormatException: Invalid Double on Tip Calculator -
Android Java NumberFormatException: Invalid Double on Tip Calculator -
am working on basic tip calculator , running next programme crash. log crash is:
06-24 01:03:43.602: e/androidruntime(27213): fatal exception: main 06-24 01:03:43.602: e/androidruntime(27213): java.lang.numberformatexception: invalid double: "android.widget.edittext@41634f08" 06-24 01:03:43.602: e/androidruntime(27213): @ java.lang.stringtoreal.invalidreal(stringtoreal.java:63) 06-24 01:03:43.602: e/androidruntime(27213): @ java.lang.stringtoreal.initialparse(stringtoreal.java:114) 06-24 01:03:43.602: e/androidruntime(27213): @ java.lang.stringtoreal.parsedouble(stringtoreal.java:263) 06-24 01:03:43.602: e/androidruntime(27213): @ java.lang.double.parsedouble(double.java:295) 06-24 01:03:43.602: e/androidruntime(27213): @ mkelleyjr.example.tippycalc.mainactivity$1.onclick(mainactivity.java:30) 06-24 01:03:43.602: e/androidruntime(27213): @ android.view.view.performclick(view.java:3540) 06-24 01:03:43.602: e/androidruntime(27213): @ android.view.view$performclick.run(view.java:14167) 06-24 01:03:43.602: e/androidruntime(27213): @ android.os.handler.handlecallback(handler.java:605) 06-24 01:03:43.602: e/androidruntime(27213): @ android.os.handler.dispatchmessage(handler.java:92) 06-24 01:03:43.602: e/androidruntime(27213): @ android.os.looper.loop(looper.java:137) 06-24 01:03:43.602: e/androidruntime(27213): @ android.app.activitythread.main(activitythread.java:4558) 06-24 01:03:43.602: e/androidruntime(27213): @ java.lang.reflect.method.invokenative(native method) 06-24 01:03:43.602: e/androidruntime(27213): @ java.lang.reflect.method.invoke(method.java:511) 06-24 01:03:43.602: e/androidruntime(27213): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:784) 06-24 01:03:43.602: e/androidruntime(27213): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:551) 06-24 01:03:43.602: e/androidruntime(27213): @ dalvik.system.nativestart.main(native method)
activity_main.xml is:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:layout_margintop="47dp" android:background="@android:color/holo_blue_light" android:text="total bill amount" android:textappearance="?android:attr/textappearancemedium" android:textcolor="@android:color/holo_orange_dark" /> <textview android:id="@+id/res" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_alignparentleft="true" android:layout_alignright="@+id/button1" android:layout_marginbottom="14dp" android:text="result : " android:textappearance="?android:attr/textappearancelarge" /> <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/res" android:layout_alignparentleft="true" android:layout_alignright="@+id/textview2" android:layout_marginbottom="34dp" android:background="@android:color/black" android:text="calculate" android:textcolor="@android:color/white" /> <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button1" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_marginbottom="96dp" android:background="@android:color/holo_green_dark" android:text="enter tip % :" android:textappearance="?android:attr/textappearancemedium" android:textcolor="@android:color/holo_red_dark" /> <edittext android:id="@+id/bill_amt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_below="@id/textview1" android:layout_margintop="41dp" android:ems="10" > <requestfocus /> </edittext> <edittext android:id="@+id/bill_percent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button1" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_marginbottom="22dp" android:ems="10" /> </relativelayout>
mainactivity.java is:
package mkelleyjr.example.tippycalc; import android.os.bundle; import android.app.activity; import android.view.menu; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final edittext amt = (edittext) findviewbyid(r.id.bill_amt); final edittext tip = (edittext) findviewbyid(r.id.bill_percent); final textview result = (textview) findviewbyid(r.id.res); button calc = (button) findviewbyid(r.id.button1); calc.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { double amount = double.parsedouble(amt.tostring()); double tip_per = double.parsedouble(tip.tostring()); double tip_cal = (amount * tip_per) / 100; result.settext("result : " + double.tostring(tip_cal)); } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); homecoming true; } }
not sure why it's crashing saying invalid double @ moment. help appreciated! in advance!
java.lang.numberformatexception: invalid double:"android.widget.edittext@41634f08"
you passing edittext
object
's reference string
in parsedouble(string)
method in double.parsedouble(amt.tostring())
, double.parsedouble(tip.tostring());
instead use
double.parsedouble(edittext.gettext().tostring())
or
double.valueof(edittext.gettext().tostring())
in code.
java android double
Comments
Post a Comment