nullpointerexception - Unable to start activity ComponentInfo in Android -
nullpointerexception - Unable to start activity ComponentInfo in Android -
the logcat details of error:
06-24 10:23:48.836: w/dalvikvm(276): threadid=1: thread exiting uncaught exception (group=0x4001d800) 06-24 10:23:48.865: e/androidruntime(276): fatal exception: main 06-24 10:23:48.865: e/androidruntime(276): java.lang.runtimeexception: unable start activity componentinfo{com.example.triviality/com.example.triviality.infoactivity}: java.lang.nullpointerexception 06-24 10:23:48.865: e/androidruntime(276): @ android.app.activitythread.performlaunchactivity(activitythread.java:2663) 06-24 10:23:48.865: e/androidruntime(276): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2679) 06-24 10:23:48.865: e/androidruntime(276): @ android.app.activitythread.access$2300(activitythread.java:125) 06-24 10:23:48.865: e/androidruntime(276): @ android.app.activitythread$h.handlemessage(activitythread.java:2033) 06-24 10:23:48.865: e/androidruntime(276): @ android.os.handler.dispatchmessage(handler.java:99) 06-24 10:23:48.865: e/androidruntime(276): @ android.os.looper.loop(looper.java:123) 06-24 10:23:48.865: e/androidruntime(276): @ android.app.activitythread.main(activitythread.java:4627) 06-24 10:23:48.865: e/androidruntime(276): @ java.lang.reflect.method.invokenative(native method) 06-24 10:23:48.865: e/androidruntime(276): @ java.lang.reflect.method.invoke(method.java:521) 06-24 10:23:48.865: e/androidruntime(276): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:868) 06-24 10:23:48.865: e/androidruntime(276): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:626) 06-24 10:23:48.865: e/androidruntime(276): @ dalvik.system.nativestart.main(native method) 06-24 10:23:48.865: e/androidruntime(276): caused by: java.lang.nullpointerexception 06-24 10:23:48.865: e/androidruntime(276): @ com.example.triviality.infoactivity.oncreate(infoactivity.java:32) 06-24 10:23:48.865: e/androidruntime(276): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1047) 06-24 10:23:48.865: e/androidruntime(276): @ android.app.activitythread.performlaunchactivity(activitythread.java:2627) 06-24 10:23:48.865: e/androidruntime(276): ... 11 more 06-24 10:23:59.235: i/process(276): sending signal. pid: 276 sig: 9
there 2 classes, quizactivity , infoactivity. if category value 2 need display quizactivity , other 2 need display infoactivity. calling quizactivity, infoactivity based on category in both programs.
quizactivity.java
package com.example.triviality; import java.util.list; import com.example.triviality.r.raw; import android.os.bundle; import android.app.activity; import android.app.alertdialog; import android.content.dialoginterface; import android.content.intent; import android.util.log; import android.view.menu; import android.view.view; import android.widget.button; import android.widget.radiobutton; import android.widget.radiogroup; import android.widget.textview; import android.media.mediaplayer; public class quizactivity extends activity { list<question> queslist; int score=0; public static int qid=0; question currentq; textview txtquestion,txtquestionnum, txtpoints,txtexplanation; radiobutton rda, rdb, rdc, rdd; public static int qno=0; button butnext; public mediaplayer mpr,mpw; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_quiz); dbhelper db=new dbhelper(this); queslist=db.getallquestions(); currentq=queslist.get(qid); txtquestionnum=(textview)findviewbyid(r.id.textview2); txtpoints=(textview)findviewbyid(r.id.textview3); txtquestion=(textview)findviewbyid(r.id.textview1); rda=(radiobutton)findviewbyid(r.id.radio0); rdb=(radiobutton)findviewbyid(r.id.radio1); rdc=(radiobutton)findviewbyid(r.id.radio2); rdd=(radiobutton)findviewbyid(r.id.radio3); butnext=(button)findviewbyid(r.id.button1); mpr = mediaplayer.create(quizactivity.this, raw.right); mpw = mediaplayer.create(quizactivity.this, raw.wrong); setquestionview(); butnext.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { if (currentq.getcategory().equals("2")) { radiogroup grp=(radiogroup)findviewbyid(r.id.radiogroup1); radiobutton answer=(radiobutton)findviewbyid(grp.getcheckedradiobuttonid()); log.d("yourans", currentq.getanswer()+" "+answer.gettext()); if(currentq.getanswer().equals(answer.gettext())) { callanswer("right"); score= score + 5; log.d("score", "your score"+score); } else { score= score - 2; callanswer("wrong"); } } else { intent intent = new intent(quizactivity.this, infoactivity.class); startactivity(intent); } } }); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_quiz, menu); homecoming true; } public void callanswer(string status) { txtpoints.settext("points: " + score); string messageval = "correct reply : "; if (status.equals("right")) { mpr.start(); messageval = "answer : "; } else { mpw.start(); } messageval = messageval + currentq.getanswer()+"\n"; messageval = messageval + "more info: " + currentq.getdetail(); alertdialog.builder alertdialog = new alertdialog.builder(this); alertdialog.settitle(" "); if (status.equals("right")) { alertdialog.seticon(r.drawable.right); } else { alertdialog.seticon(r.drawable.wrong); } alertdialog.setmessage(messageval); alertdialog.setpositivebutton("ok", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog,int which) { if(qno<10){ currentq=queslist.get(qid); setquestionview(); }else{ intent intent = new intent(quizactivity.this, resultactivity.class); bundle b = new bundle(); b.putint("score", score); //your score intent.putextras(b); //put score next intent startactivity(intent); finish(); } } }); alertdialog.show(); } private void setquestionview() { if (currentq.getcategory().equals("2")) { setcontentview(r.layout.activity_quiz); txtquestionnum.settext("question no: "+ ++qno); txtpoints.settext("points: " + score); txtquestion.settext(currentq.getquestion()); rda.settext(currentq.getopta()); rdb.settext(currentq.getoptb()); rdc.settext(currentq.getoptc()); rdd.settext(currentq.getoptd()); qid++; } else { intent intent = new intent(this, infoactivity.class); startactivity(intent); } } }
infoactivity.java
package com.example.triviality; import java.util.list; import com.example.triviality.r.raw; import android.os.bundle; import android.app.activity; import android.app.alertdialog; import android.content.dialoginterface; import android.content.intent; import android.util.log; import android.view.menu; import android.view.view; import android.widget.button; import android.widget.radiobutton; import android.widget.radiogroup; import android.widget.textview; import android.media.mediaplayer; public class infoactivity extends activity { list<question> queslist; int score=0; question currentq; textview txtquestion,txtquestionnum, txtpoints,txtexplanation; radiobutton rda, rdb, rdc, rdd; public static int qno=0; button butnext; public mediaplayer mpr,mpw; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_info); currentq=queslist.get(quizactivity.qid); txtquestion=(textview)findviewbyid(r.id.textview1); txtexplanation=(textview)findviewbyid(r.id.textview6); setquestionview(); butnext.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(infoactivity.this, quizactivity.class); startactivity(intent); } }); } private void setquestionview() { log.d("category",currentq.getcategory()); if (currentq.getcategory().equals("2")) { intent intent = new intent(infoactivity.this, quizactivity.class); startactivity(intent); } else { txtquestion.settext(currentq.getquestion()); txtexplanation.settext(currentq.getcategory()); quizactivity.qid++; } } }
you have declared
list<question> queslist;
it not initialized , when have currentq=queslist.get(quizactivity.qid)
leads nullpointerexception
. if initialize list still not populated items.
note : not utilize setcontentview differennt layout set same activity. not idea. each activity has own ui , lifecycle of own.
android nullpointerexception
Comments
Post a Comment