Unfortunately GeoQuiz has stopped

#1

I am having problems running the GeoQuiz app. The first thing I did was comment out the updateQuestion() method and method calls. rerun and it works. Something is causing this to throw NullPointerException error.Below I have included, logcat,TrueFalse.java, QuizActivity.java, strings.xml,and activety_quiz.xml. I looked at other possibilities that other users posted with similar issues. I did not see anything in other Posts that solves my problem. On my Emulator the app crashes. Please help someone?

Thanks David

04-26 09:30:42.863    2232-2232/geoquiz.aandroid.bignerdranch.com.geoquiz E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: geoquiz.aandroid.bignerdranch.com.geoquiz, PID: 2232
    java.lang.RuntimeException: Unable to start activity ComponentInfo{geoquiz.aandroid.bignerdranch.com.geoquiz/geoquiz.aandroid.bignerdranch.com.geoquiz.QuizActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null object reference
            at geoquiz.aandroid.bignerdranch.com.geoquiz.QuizActivity.updateQuestion(QuizActivity.java:32)
            at geoquiz.aandroid.bignerdranch.com.geoquiz.QuizActivity.onCreate(QuizActivity.java:81)
            at android.app.Activity.performCreate(Activity.java:5937)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

TrueFalse.java

[code]package geoquiz.aandroid.bignerdranch.com.geoquiz;

/**

  • Created by David on 4/10/2015.
    */
    public class TrueFalse {
    private int mQuestion;
    private boolean mTrueQuestion;
    public TrueFalse(int question, boolean trueQuestion){
    mQuestion = question;
    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;
    }
    }
    [/code]
    QuizActivity.java

[code]package geoquiz.aandroid.bignerdranch.com.geoquiz;
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 static android.view.View.OnClickListener;

public class QuizActivity extends ActionBarActivity {
private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private TextView mQuestionTextView;

//Add questions to TrueFalse class here
private TrueFalse[] mQuestionBank = new TrueFalse[]{
    new TrueFalse(R.string.question_oceans,true),
    new TrueFalse(R.string.question_mideast,true),
    new TrueFalse(R.string.question_africa,true),
    new TrueFalse(R.string.question_america,true),
    new TrueFalse(R.string.question_asia,true),
};
private int  mCurrentIndex= 0;
private void updateQuestion(){
    int question = mQuestionBank[mCurrentIndex].getQuestion();
    mQuestionTextView.setText(question);
}

//check answers
private void checkAnswer(boolean userPressedTrue){
  boolean answerTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
  int messageResId = 0;
  if(userPressedTrue == answerTrue){
     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);

    //Set text view here
    mTrueButton = (Button) findViewById(R.id.true_button);
    mTrueButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //Toast.makeText(QuizActivity.this,R.string.correct_toast,Toast.LENGTH_SHORT).show();
            checkAnswer(true);
        }
    });

    mFalseButton = (Button)findViewById(R.id.false_button);
    mFalseButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
             //Toast.makeText(QuizActivity.this,R.string.incorrect_toast, Toast.LENGTH_SHORT).show();
             checkAnswer(false);
        }
    });

           mNextButton = (Button)findViewById(R.id.next_button);
           mNextButton.setOnClickListener(new OnClickListener(){
   //@Override
    public void onClick(View v){
        mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
       updateQuestion();
    }


    });
       updateQuestion();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_quiz, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);

}

}
[/code]

strings.xml

<resources> <string name="app_name">GeoQuiz</string> <string name="true_button">True</string> <string name="false_button">False</string> <string name="correct_toast">Correct!</string> <string name="incorrect_toast">Incorrect!</string> <string name="next_button">Next</string> <string name="action_settings">Settings</string> <string name="question_oceans">The Pacific Ocean is larger than the Atlantic Ocean.</string> <string name="question_mideast">The Suez Canal connects the Red Sea and the Indian Ocean.</string> <string name="question_africa">The source of the Nile River is in Egypt</string> <string name="question_america">The Amazon river is the longest river in the Americas.</string> <string name="question_asia">Lake Baikal is the world\'s oldest and deepest freshwater lake</string> </resources>

activety_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"/> <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> <Button android:id="@+id/next_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/next_button" android:drawableRight="@drawable/arrow_right" android:drawablePadding="4dp" /> </LinearLayout>

#2

I believed i solved my issue. I saw a message saying mQuestionTextView was not getting assigned. I changed one line of code and my errors went away and it loads like a champ! yay!

Below is what I changed:

private void updateQuestion(){
        int question = mQuestionBank[mCurrentIndex].getQuestion();
       mQuestionTextView = (TextView) this.findViewById(R.id.question_text_view);
       mQuestionTextView.setText(question);
    }

If anyone else runs across another way to solve the problem, I would love to here it?

David :smiley: