eclipse - android app is crashing -
using big nerd ranch tutorial book , finished chapter or have 1 page left there points in quizactivity.java i'm not sure if i'm supposed delete.
my quizactivity.java is
package com.bignerdranch.android.geoquiz; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v7.app.actionbaractivity; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.textview; import android.widget.toast; public class quizactivity extends actionbaractivity { private button mtruebutton; private button mfalsebutton; private button mnextbutton; private textview mquestiontextview; private truefalse[] mquestionbank = new truefalse [] { new truefalse(r.string.question_oceans, true), new truefalse(r.string.question_mideast, false), new truefalse(r.string.question_africa, false), new truefalse(r.string.question_americas, true), new truefalse(r.string.question_asia, true), }; private int mcurrentindex = 0; private void updatequestion() { int question = mquestionbank[mcurrentindex].getquestion(); mquestiontextview.settext(question); } private void checkanswer(boolean userpressedtrue) { boolean answeristrue = mquestionbank[mcurrentindex].istruequestion(); int messageresid = 0; 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 oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_quiz); mquestiontextview = (textview)findviewbyid(r.id.question_text_view); mtruebutton = (button)findviewbyid(r.id.true_button); mtruebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(quizactivity.this, r.string.correct_toast, toast.length_short).show(); } }); mfalsebutton = (button)findviewbyid(r.id.false_button); mfalsebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { toast.maketext(quizactivity.this, r.string.incorrect_toast, toast.length_short).show(); } }); mnextbutton = (button)findviewbyid(r.id.next_button); mnextbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { mcurrentindex = (mcurrentindex + 1) % mquestionbank.length; } }); updatequestion(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.quiz, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } /** * placeholder fragment containing simple view. */ public static class placeholderfragment extends fragment { public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.activity_quiz, container, false); return rootview; } } }
my geoquiz manifest is
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.bignerdranch.android.geoquiz" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="16" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.bignerdranch.android.geoquiz.quizactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> </application> </manifest>
my activity_quiz.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/question_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="24dp" /> <button android:id="@+id/next_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/next_button" /> <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>
my truefalse.java
package com.bignerdranch.android.geoquiz; public class truefalse { private int mquestion; private boolean mtruequestion; public truefalse(int question, boolean truequestion) { mquestion = getquestion(); mtruequestion = truequestion; } public int getquestion() { return mquestion; } public void setquestion(int question) { mquestion = question; } public boolean istruequestion() { return mtruequestion; } public void settruequestion(boolean truequestion) { mtruequestion = truequestion; } }
i believe you're getting resourcenotfoundexception
,
please note array of quizactivity
private truefalse[] mquestionbank = new truefalse [] { new truefalse(r.string.question_oceans, true), new truefalse(r.string.question_mideast, false), new truefalse(r.string.question_africa, false), new truefalse(r.string.question_americas, true), new truefalse(r.string.question_asia, true), };
the cause starts here,
private int mquestion; // default value of integer 0 public truefalse(int question, boolean truequestion) { mquestion = getquestion(); // you're assigning assigning value of mquestion mtruequestion = truequestion; }
question
parameter never been passed field mquestion
.
to fix, must assign question
mquestion
variable.
public truefalse(int question, boolean truequestion) { mquestion = question; mtruequestion = truequestion; }
Comments
Post a Comment