android - why editText make program stopped if didn't input anything -
i new. have 2 edittext , 1 textview, 1 button. want sum 2 input. success if input edittext. if didn't input, aplication stopped.
package panda.c; import android.app.activity; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; public class calculate extends activity implements onclicklistener { /** called when activity first created. */ edittext input1,input2; button sum; textview total; public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.calcu); input1 = (edittext) findviewbyid(r.id.input1); input2 = (edittext) findviewbyid(r.id.input2); total = (edittext) findviewbyid(r.id.total); sum = (button) findviewbyid(r.id.sum); sum.setonclicklistener(this); } public void onclick(view v) { int inputt1 = integer.parseint(input1.gettext().tostring()); int inputt2 = integer.parseint(input2.gettext().tostring()); switch (v.getid()) { case r.id.sum: sum.settext(integer.tostring(inputt1+inputt2)); break; default: break; }}}
i didn't input anything. clicked sum, , program stopped. if input edittext, clicked sum, work. why? want if not input, , click sum, application still run.
the code fragment in onclick() method following:
int inputt1 = 0; int inputt2 = 0; try { inputt1 = integer.parseint(input1.gettext().tostring()); inputt2 = integer.parseint(input2.gettext().tostring()); } catch (numberformatexception e) { //notify user here somehow or leave empty //or return, if don't want sum default values }
Comments
Post a Comment