java - Execution of empty space in calculator crashes android -
i beginner in android. made calculator. works fine when there value in boxes if they're empty crashes. have tried many things
the first loop divide has problem. haven't implemented on other arithmetic operations,just divide.
package com.example.bilal.aaa; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.toast; public class mainactivity extends actionbaractivity { edittext t,t2; textview t3; button b1,b2,b3,b4,b6; @override protected void oncreate(bundle savedinstancestate) { // super.oncreate(savedinstancestate); // setcontentview(r.layout.home); super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); t = (edittext)findviewbyid(r.id.edittext); t2 = (edittext)findviewbyid(r.id.edittext2); t3 = (textview)findviewbyid(r.id.edittext3); button b1 = (button) findviewbyid(r.id.button1); b1.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { create(v); } }); button b6 = (button) findviewbyid(r.id.button6); button b2 = (button) findviewbyid(r.id.button2); b2.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { create(v); } }); button b3 = (button) findviewbyid(r.id.button3); b3.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { create(v); } }); button b4 = (button) findviewbyid(r.id.button4); b4.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { create(v); } }); try{ b1.setonclicklistener((view.onclicklistener) this); b2.setonclicklistener((view.onclicklistener) this); b3.setonclicklistener((view.onclicklistener) this); b4.setonclicklistener((view.onclicklistener) this); b6.setonclicklistener((view.onclicklistener)this); } catch(exception e){ toast.maketext(getapplicationcontext(), "please add number", toast.length_long).show(); } } string a,b; double a1,b1; double result; int aa1,bb1; public void create(view v) { switch (v.getid()) { case r.id.button1: = t.gettext().tostring(); b=t2.gettext().tostring(); a1=double.parsedouble(a); b1=double.parsedouble(b); if (a.equals(null)) { toast.maketext(getapplicationcontext(), "enter first number", toast.length_long).show(); // create(v); } else if (b.equals(null)) { toast.maketext(getapplicationcontext(), "enter second number", toast.length_long).show(); // create(v); } else if (a1.equals(null)) { toast.maketext(getapplicationcontext(), "enter first number", toast.length_long).show(); // create(v); } else if (b1.equals(null)) { toast.maketext(getapplicationcontext(), "enter second number", toast.length_long).show(); // create(v); } else { result=a1/b1; answer(v); } // log.e("exception", a); // log.e("exception",b);} // else{ // // // toast.maketext(getbasecontext(), "value"+a1, toast.length_long).show(); // toast.maketext(getbasecontext(), "value"+b1, toast.length_long).show(); // result=a1/b1; // answer(v); // log.e("exception","a null"); // } // if(a.equals(null)){ // log.e("exception",a); // log.e("exception",b); // toast.maketext(getapplicationcontext(), "enter first number", toast.length_long).show(); // create(v); // // // } // else{ // if(b.equals(null)){ // log.e("exception","a null"); // toast.maketext(getapplicationcontext(), "enter second number", toast.length_long).show(); // create(v); // // } // else { //// a1=double.parsedouble(a); //// b1=double.parsedouble(b); // // toast.maketext(getbasecontext(), "value"+a1, toast.length_long).show(); // toast.maketext(getbasecontext(), "value"+b1, toast.length_long).show(); // result=a1/b1; // answer(v); // // } // // } break; case r.id.button2: = t.gettext().tostring(); b=t2.gettext().tostring(); a1=double.parsedouble(a); b1=double.parsedouble(b); if(a!=null){ if(b!=null){ result=a1*b1; answer(v); } else { toast.maketext(getapplicationcontext(), "enter second number", toast.length_long).show(); } } else { toast.maketext(getapplicationcontext(), "enter first number", toast.length_long).show(); } { toast.maketext(getapplicationcontext(), "enter first number", toast.length_long).show(); } break; case r.id.button3: = t.gettext().tostring(); b=t2.gettext().tostring(); a1=double.parsedouble(a); b1=double.parsedouble(b); if(a1 != 0){ if(b1 != 0){ result=a1+b1; answer(v); } else { toast.maketext(getapplicationcontext(), "enter second number", toast.length_long).show(); } } else { toast.maketext(getapplicationcontext(), "enter first number", toast.length_long).show(); } break; case r.id.button4: = t.gettext().tostring(); b=t2.gettext().tostring(); a1=double.parsedouble(a); b1=double.parsedouble(b); if(a1 != 0){ if(b1 != 0){ result=a1-b1; answer(v); } else { toast.maketext(getapplicationcontext(), "enter second number", toast.length_long).show(); } } else { toast.maketext(getapplicationcontext(), "enter first number", toast.length_long).show(); } break; default: toast.maketext(this, "invalid option", toast.length_short).show(); break; } } public void clear(view v) { t.settext(""); t2.settext(""); } public void answer(view v) { string tmpstring = double.tostring(result); t3.settext(tmpstring); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
message logcat
05-12 15:01:20.709 5624-5624/com.example.bilal.aaa d/openglrenderer﹕ enabling debug mode 0 05-12 15:01:20.869 5624-5624/com.example.bilal.aaa e/dalvikvm﹕ dvmresumegc(0xf126, 0) called (f=0x1) 05-12 15:01:31.504 5624-5624/com.example.bilal.aaa d/androidruntime﹕ shutting down vm 05-12 15:01:31.504 5624-5624/com.example.bilal.aaa w/dalvikvm﹕ threadid=1: thread exiting uncaught exception (group=0x420b5c08) 05-12 15:01:31.514 5624-5624/com.example.bilal.aaa e/androidruntime﹕ fatal exception: main process: com.example.bilal.aaa, pid: 5624 java.lang.numberformatexception: invalid double: "" @ java.lang.stringtoreal.invalidreal(stringtoreal.java:63) @ java.lang.stringtoreal.parsedouble(stringtoreal.java:248) @ java.lang.double.parsedouble(double.java:295) @ com.example.bilal.aaa.mainactivity.create(mainactivity.java:102) @ com.example.bilal.aaa.mainactivity$1.onclick(mainactivity.java:30) @ android.view.view.performclick(view.java:4637) @ android.view.view$performclick.run(view.java:19422) @ android.os.handler.handlecallback(handler.java:733) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5586) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1268) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1084) @ dalvik.system.nativestart.main(native method) 05-12 15:01:57.889 5733-5733/com.example.bilal.aaa e/dalvikvm﹕ dvmpausegc(applaunch) called - cookie=0x835c (f=0x1)
case r.id.button1: = t.gettext().tostring(); b=t2.gettext().tostring(); a1=double.parsedouble(a); b1=double.parsedouble(b);
this part should check , b string.isempty(), , t2 if it's not null:
case r.id.button1: if(t != null && !t.gettext().tostring().isempty()) { = t.gettext().tostring(); a1=double.parsedouble(a); } if(t2 != null && !t2.gettext().tostring().isempty()) { b = t2.gettext().tostring(); b1=double.parsedouble(b); }
Comments
Post a Comment