java - Android app crashes after launching intent and using setText() in TextView -



java - Android app crashes after launching intent and using setText() in TextView -

i'm trying launch activity using intents , setting values in textviews in launched activity, when seek app crashes. heres activity i'm trying launch. crashes when seek set text in lastly 2 code lines

public class gameover extends activity { textview correcttxt; textview incorrecttxt; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); intent intent = getintent(); int right = intent.getintextra("correct", 0); int wrong = intent.getintextra("incorrect", 0); correcttxt = (textview)findviewbyid(r.id.correct_txt); incorrecttxt = (textview)findviewbyid(r.id.incorrect_txt); correcttxt.settext("" + correct); incorrecttxt.settext("" + incorrect); } }

and activity that's launching. create utilize of intent in truebutton onclicklistener method:

package com.example.beithia.geotest; import android.app.activity; import java.util.*; import android.content.intent; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.button; import android.widget.textview; import android.widget.toast; import android.content.intent; public class testactivity extends activity { private button truebutton; private button falsebutton; private textview questiontextview; private textview correctnum; private textview incorrectnum; private textview avgscore; int numofcorrect = 0; int numofincorrect = 0; double num = 1; private truefalse[] questionbank = new truefalse[] { new truefalse(r.string.question_oceans, true), new truefalse(r.string.question_mideast, false), new truefalse(r.string.question_americas, true), new truefalse(r.string.question_asia,true), new truefalse(r.string.question_channel_islands,true), new truefalse(r.string.question_china,false), new truefalse(r.string.question_mexico,true), new truefalse(r.string.question_turkey,false), new truefalse(r.string.question_everest,false), new truefalse(r.string.question_colombia,false), new truefalse(r.string.question_vatican,true), new truefalse(r.string.question_nile,true), }; private int currentindex = 0; private void updatequestion() { int question = questionbank[currentindex].getquestion(); questiontextview.settext(question); } private void checkanswer(boolean userpressedtrue) { boolean answeristrue = questionbank[currentindex].istruequestion(); int msgid = 0; if (userpressedtrue == answeristrue) { msgid = r.string.correct_toast; numofcorrect++; } else { msgid = r.string.incorrect_toast; numofincorrect++; } toast.maketext(this,msgid,toast.length_short).show(); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); questiontextview = (textview)findviewbyid(r.id.question_text_view); correctnum = (textview)findviewbyid(r.id.correct_num); incorrectnum = (textview)findviewbyid(r.id.incorrect_num); avgscore = (textview)findviewbyid(r.id.average_score); truebutton = (button) findviewbyid(r.id.true_button); truebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { checkanswer(true); double score = (numofcorrect / num) * 100; currentindex = (currentindex + 1) % questionbank.length; correctnum.settext("correct: " + numofcorrect); incorrectnum.settext("incorrect: " + numofincorrect); avgscore.settext("score: " + string.format("%.2f", score) + "%"); updatequestion(); num++; if(num > 10) { intent intent = new intent(testactivity.this, gameover.class); intent.putextra("correct", numofcorrect); intent.putextra("incorrect", numofincorrect); startactivity(intent); } } }); falsebutton = (button) findviewbyid(r.id.false_button); falsebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { checkanswer(false); double score = (numofcorrect / num) * 100; currentindex = (currentindex + 1) % questionbank.length; correctnum.settext("correct: " + numofcorrect); incorrectnum.settext("incorrect: " + numofincorrect); avgscore.settext("score: " + string.format("%.2f", score) + "%"); updatequestion(); num++; } }); updatequestion(); } }

here's layout activity_main.xml:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <textview android:id="@+id/correct_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="#ff00ff12" /> <textview android:id="@+id/incorrect_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="#ffff5440" /> <textview android:id="@+id/average_score" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textcolor="#ffff8c1a" /> <textview android:id="@+id/question_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="24dp" /> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <button android:id="@+id/true_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/true_button"/> <button android:id="@+id/false_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/false_button"/> </linearlayout> </linearlayout>

and layout activity_game_over.xml:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <textview android:id="@+id/correct_txt" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <textview android:id="@+id/incorrect_txt" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </linearlayout>

i can work if utilize setcontentview(r.layout.activity_game_over); when seek launch main activity 1 time again starts gameover activity, should start geoquiz activity instead.

in gameover activity don't set content view setcontentview(), hence textviews cannot found, findviewbyid returns null, hence nullpointerexception when invoking settext().

java android android-intent crash

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -