Android Back Button error

first Question is the Question_oceans see code below. trying to have user note that it can not get "Prevoius" question when button is pressed because it was the first question" im getting an error.

private Question[] mQuestionBank = new Question[]{
        new Question(R.string.question_oceans, true),
        new Question(R.string.question_mideast, false),
        new Question(R.string.question_africa, false),
        new Question(R.string.question_americas, true),
        new Question(R.string.question_asia, true),
};

private int mCurrentIndex = 0;

private void updateQuestion() {
    int question = mQuestionBank[mCurrentIndex].getTextResId();
    mQuestionTextView.setText(question);
}

private void lastQuestion(boolean userPressedPrevious) {
    boolean answerIsYes = mQuestionBank[mCurrentIndex].isAnswerTrue();

    int messageQuesId = 0;

    if (userPressedPrevious == answerIsYes) {
        messageQuesId = R.string.not_previous;
    } else {
        updateQuestion();
    }

    Toast.makeText(this, messageQuesId, Toast.LENGTH_SHORT).show();
}

private void checkAnswer(boolean userPressedTrue) {
    boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();

    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) {
            checkAnswer(true);
        }

    });

    mFalseButton = (Button) findViewById(R.id.false_button);
    mFalseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            checkAnswer(false);
        }
    });

    mPreviousButton = (Button) findViewById(R.id.previous_button);
    mPreviousButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
            updateQuestion();
            lastQuestion(true);
        }
    });
    updateQuestion();

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

    updateQuestion();

Answers


Maybe you should use a try catch statement. If you run it again, you will probably see a logcat error telling you what the issue it. In catch, you could make a Toast saying, "Sorry, previous question not available". Good luck!


Need Your Help

iOS: Debugging simple UI lags (based on CoreData I guess)

ios objective-c core-data uicollectionview uiswitch

I'm testing my app currently especially on an iPhone 4s. And some simple UI tasks seem laggy and delayed, sutch as selecting an image in a collection view or changing the state of a UI checkbox. Is...

How do you enable mysql.trace_mode for php?

php mysql .htaccess

I would like to have it so if there is a mysql error it throws a php style error.