java - Android: whenever a edittext field is left empty, program closes giving exception -
program sudenly close when edittext left empty when ever pressed button move next activity. here code.
package cme.ws.com.ws.cme; import android.app.activity; import android.content.context; import android.content.intent; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.toast; /** * created waqas aamer on 5/27/2015. */ public class areaactivity extends activity { int plotarea, coveredarea; int brickprice, blockprice, cementprice, sandprice, crushprice, ironprice; int brickprice1, blockprice1, cementprice1, sandprice1, crushprice1, ironprice1; int plotarea1, coveredarea1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_area); { final context context = getapplicationcontext(); final int duration = toast.length_short; button movenextarea=(button) findviewbyid(r.id.buttonnextexteriorwall); movenextarea.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { intent intent = getintent(); if (null != intent) { // test = intent.getstringextra("nameofvariable"); brickprice1 = intent.getintextra("anything00", brickprice); blockprice1 = intent.getintextra("anything11", blockprice); sandprice1 = intent.getintextra("anything22", sandprice); ironprice1 = intent.getintextra("anything33", ironprice); cementprice1 = intent.getintextra("anything44", cementprice); crushprice1 = intent.getintextra("anything55", crushprice); } edittext areanumber1 = (edittext) findviewbyid(r.id.edittextplotarea); if(areanumber1 == null) { toast toast = toast.maketext(context, "please fill plot area field ", duration); toast.show(); } else { plotarea1 = integer.parseint(areanumber1.gettext().tostring()); } edittext areanumber2 = (edittext) findviewbyid(r.id.edittextcoveredarea); if(areanumber2 == null) { toast toast1 = toast.maketext(context, "please fill covered area field ", duration); toast1.show(); } else { coveredarea1 = integer.parseint(areanumber2.gettext().tostring()); } int sqrt = (int) math.sqrt(plotarea1); int oneside = sqrt; intent secondactivity = new intent (getapplicationcontext(), exteriorwallactivity.class); secondactivity.putextra("anything000", brickprice1); secondactivity.putextra("anything111", blockprice1); secondactivity.putextra("anything222", cementprice1); secondactivity.putextra("anything333", sandprice1); secondactivity.putextra("anything444", ironprice1); secondactivity.putextra("anything555", crushprice1); secondactivity.putextra("anything", oneside); secondactivity.putextra("anything1", coveredarea1); startactivity(secondactivity); } }); } }
}
i want whenver edittext box left empty .the variable in whcih storing value automatically stores 0. eroor log is
07-05 23:20:42.206 14348-14348/cme.ws.com.ws.cme e/androidruntime﹕ fatal exception: main java.lang.numberformatexception: unable parse '' integer @ java.lang.integer.parseint(integer.java:412) @ java.lang.integer.parseint(integer.java:382) @ cme.ws.com.ws.cme.areaactivity$1.onclick(areaactivity.java:52) @ android.view.view.performclick(view.java:2408) @ android.view.view$performclick.run(view.java:8816) @ android.os.handler.handlecallback(handler.java:587) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:123) @ android.app.activitythread.main(activitythread.java:4627) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:521) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:868) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:626) @ dalvik.system.nativestart.main(native method)
your edittext
fields cannot equals null
because in previous line of verification have initialized them. instead of using if (areanumber1 == null)
, if (areanumber2 == null)
, check edittext
this:
boolean isempty(edittext textfield) { return textfield.gettext().tostring().trim().length() == 0; }
if function return false
means edittext
not empty , return true
means edittext
empty.
Comments
Post a Comment