android - Returning to the main activity after exiting a sub activity deactivates two of my buttons -
so, i'm working through big nerd ranch stuff @ moment on android, , i've wrapped largely first application, great fun, whatever, problem i'm having after implementing cheat button quiz, when return cheating screen, true , false buttons no longer function.
there's click registered, ceases anything.
also having bug if turn horizontally, whole app crashes, i'll post code of 2 activities , see get.
private void checkanswer(boolean userpressedtrue){ boolean answeristrue = mquestionbank[mcurrentindex].istruequestion(); int messageresid=0; if(mischeater){ messageresid=r.string.judgement_toast; } else { if (userpressedtrue==answeristrue) { messageresid=r.string.correct_toast; } else { messageresid=r.string.incorrect_toast; } toast.maketext(this, messageresid, toast.length_short) .show(); } } @override protected void onactivityresult(int requestcode, int resultcode, intent data){ if(data==null){ return; } mischeater=data.getbooleanextra(cheatactivity.extra_answer_shown, false); } @override protected void oncreate(bundle savedinstancestate) { if(savedinstancestate!=null){ mcurrentindex=savedinstancestate.getint(key_index, 0); } super.oncreate(savedinstancestate); log.d(tag, "oncreate(bundle) called"); setcontentview(r.layout.activity_quiz); mcheatbutton=(button)findviewbyid(r.id.cheat_button); mcheatbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent = new intent(quizactivity.this, cheatactivity.class); boolean answeristrue=mquestionbank[mcurrentindex].istruequestion(); i.putextra(cheatactivity.extra_answer_is_true, answeristrue); startactivityforresult(i,0); } }); mtruebutton = (button)findviewbyid(r.id.true_button); mtruebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { checkanswer(true); } }); mfalsebutton = (button)findviewbyid(r.id.false_button); mfalsebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { checkanswer(false); } }); @override public void onsaveinstancestate(bundle savedinstancestate){ super.onsaveinstancestate(savedinstancestate); log.i(tag, "onsaveinstancestate"); savedinstancestate.putint(key_index, mcurrentindex); }
package com.example.geoquiz; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.textview; public class cheatactivity extends activity { private boolean mansweristrue; public static final string extra_answer_is_true = "com.example.geoquiz.answer_is_true"; public static final string extra_answer_shown="com.example.geoquiz.answer_shown"; private textview manswertextview; private button mshowanswer; private void setanswershownresult(boolean isanswershown){ intent data = new intent(); data.putextra(extra_answer_shown, isanswershown); setresult(result_ok, data); } @override protected void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); setcontentview(r.layout.activity_cheat); mansweristrue=getintent().getbooleanextra(extra_answer_is_true, false); manswertextview =(textview)findviewbyid(r.id.answertextview); setanswershownresult(false); mshowanswer=(button)findviewbyid(r.id.showanswerbutton); mshowanswer.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (mansweristrue) { manswertextview.settext(r.string.true_button); } else { manswertextview.settext(r.string.false_button); } setanswershownresult(true); } }); } }
on checking logcat, i've found null pointer exception regarding horizontal orientation problem.
08-18 18:39:30.398: e/androidruntime(24316): fatal exception: main 08-18 18:39:30.398: e/androidruntime(24316): process: com.example.geoquiz, pid: 24316 08-18 18:39:30.398: e/androidruntime(24316): java.lang.runtimeexception: unable start activity componentinfo{com.example.geoquiz/com.example.geoquiz.quizactivity}: java.lang.nullpointerexception 08-18 18:39:30.398: e/androidruntime(24316): @ android.app.activitythread.performlaunchactivity(activitythread.java:2198) 08-18 18:39:30.398: e/androidruntime(24316): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2257) 08-18 18:39:30.398: e/androidruntime(24316): @ android.app.activitythread.handlerelaunchactivity(activitythread.java:3805) 08-18 18:39:30.398: e/androidruntime(24316): @ android.app.activitythread.access$900(activitythread.java:139) 08-18 18:39:30.398: e/androidruntime(24316): @ android.app.activitythread$h.handlemessage(activitythread.java:1216) 08-18 18:39:30.398: e/androidruntime(24316): @ android.os.handler.dispatchmessage(handler.java:102) 08-18 18:39:30.398: e/androidruntime(24316): @ android.os.looper.loop(looper.java:136) 08-18 18:39:30.398: e/androidruntime(24316): @ android.app.activitythread.main(activitythread.java:5086) 08-18 18:39:30.398: e/androidruntime(24316): @ java.lang.reflect.method.invokenative(native method) 08-18 18:39:30.398: e/androidruntime(24316): @ java.lang.reflect.method.invoke(method.java:515) 08-18 18:39:30.398: e/androidruntime(24316): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:785) 08-18 18:39:30.398: e/androidruntime(24316): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:601) 08-18 18:39:30.398: e/androidruntime(24316): @ dalvik.system.nativestart.main(native method) 08-18 18:39:30.398: e/androidruntime(24316): caused by: java.lang.nullpointerexception 08-18 18:39:30.398: e/androidruntime(24316): @ com.example.geoquiz.quizactivity.oncreate(quizactivity.java:116) 08-18 18:39:30.398: e/androidruntime(24316): @ android.app.activity.performcreate(activity.java:5248) 08-18 18:39:30.398: e/androidruntime(24316): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1110) 08-18 18:39:30.398: e/androidruntime(24316): @ android.app.activitythread.performlaunchactivity(activitythread.java:2162) 08-18 18:39:30.398: e/androidruntime(24316): ... 12 more
the lack of button function diagnosed using logcat.v , printing boolean value of mischeater in logcat. after making sure boolean value prints, , what, @ point of answer checking is, rewrote checkanswer logic check value - problem prior in code it's checking if variable exists seems.
anyway, fixed code
private void checkanswer(boolean userpressedtrue){ boolean answeristrue = mquestionbank[mcurrentindex].istruequestion(); int messageresid=0; if(mischeater==true){ messageresid=r.string.judgement_toast; } else if (userpressedtrue==answeristrue) { messageresid=r.string.correct_toast; } else { messageresid=r.string.incorrect_toast; } log.v(tag, boolean.tostring(mischeater)); toast.maketext(this, messageresid, toast.length_short) .show(); }
the horizontal issue due me having landscape layout , not having cheat button declared in xml.
Comments
Post a Comment