java - Android quiz app crashing -


i developing simple quiz app , i'm having problem think codes. crashes when click submit button if text fields left blank. here's code.

public class quiz extends activity {  button submit; edittext e1,e2,e3,e4,e5; int ctr; string msg = "", msg1 = "";  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_quiz);     submit=(button)findviewbyid(r.id.button1);     e1=(edittext)findviewbyid(r.id.edittext1);     e2=(edittext)findviewbyid(r.id.edittext2);     e3=(edittext)findviewbyid(r.id.edittext3);     e4=(edittext)findviewbyid(r.id.edittext4);     e5=(edittext)findviewbyid(r.id.edittext5);     submit.setonclicklistener(new view.onclicklistener() {            @override           public void onclick(view arg0){               string ans1 = string.valueof(e1.gettext());               int ans11 = integer.parseint(ans1);               string ans2 = string.valueof(e2.gettext());               int ans22 = integer.parseint(ans2);               string ans3 = string.valueof(e3.gettext());               int ans33 = integer.parseint(ans3);               string ans4 = string.valueof(e4.gettext());               int ans44 = integer.parseint(ans4);               string ans5 = string.valueof(e5.gettext());               int ans55 = integer.parseint(ans5);                if(e1.gettext().tostring().equals("")||e2.gettext().tostring().equals("")||e3.gettext().tostring().equals("")||e4.gettext().tostring().equals("")||e5.gettext().tostring().equals("")){                     msg1 = "text fields cannot empty.";                      toast.maketext(quiz.this, msg1, toast.length_long).show();                     }                else{               if(ans11==4){                   ctr++;               }               if(ans22==3){                   ctr++;               }               if(ans33==1){                   ctr++;               }               if(ans44==2){                   ctr++;               }               if(ans55==5){                   ctr++;               }               msg = "your score " + ctr;                  toast.maketext(quiz.this, msg, toast.length_long).show();               }             }       }); } 

the logcat says:

10-06 21:47:35.888: e/androidruntime(1759): fatal exception: main 10-06 21:47:35.888: e/androidruntime(1759): java.lang.numberformatexception: unable parse '' integer 10-06 21:47:35.888: e/androidruntime(1759):     @ java.lang.integer.parseint(integer.java:362) 10-06 21:47:35.888: e/androidruntime(1759):     @ java.lang.integer.parseint(integer.java:332) 10-06 21:47:35.888: e/androidruntime(1759):     @ com.example.capslock.quiz$1.onclick(quiz.java:39) 10-06 21:47:35.888: e/androidruntime(1759):     @ android.view.view.performclick(view.java:2485) 10-06 21:47:35.888: e/androidruntime(1759):     @ android.view.view$performclick.run(view.java:9080) 10-06 21:47:35.888: e/androidruntime(1759):     @ android.os.handler.handlecallback(handler.java:587) 10-06 21:47:35.888: e/androidruntime(1759):     @ android.os.handler.dispatchmessage(handler.java:92) 10-06 21:47:35.888: e/androidruntime(1759):     @ android.os.looper.loop(looper.java:130) 10-06 21:47:35.888: e/androidruntime(1759):     @ android.app.activitythread.main(activitythread.java:3683) 10-06 21:47:35.888: e/androidruntime(1759):     @ java.lang.reflect.method.invokenative(native method) 10-06 21:47:35.888: e/androidruntime(1759):     @ java.lang.reflect.method.invoke(method.java:507) 10-06 21:47:35.888: e/androidruntime(1759):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:839) 10-06 21:47:35.888: e/androidruntime(1759):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:597) 10-06 21:47:35.888: e/androidruntime(1759):     @ dalvik.system.nativestart.main(native method) 

try string edittext

edittext.gettext().tostring(); 

if want compare string string make this:

if(ans1.equals("4")){...} 

if want integer edittext try this

int a11 = 0; try {   a11 = integer.parseint(edittext.gettext().tostring()); }catch(numberformatexception e){   } 

so not exception, when there no string or litteral string in edittext

i right, tried parse integer "" :-d


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -