I am trying to see if the user is on the last question and if so display a toast saying the percentage correct if any.
Here is my code for the project.
package com.bignerdranch.android.geoquizuse;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class QuizActivity extends AppCompatActivity {
private Button TrueButton;
private Button FalseButton;
private ImageButton PreviousButton;
private ImageButton NextButton;
private TextView QuestionTextView;
private final static String TAG = "QuizActivity";
private final static String KEY_INDEX = "index";
private int Percentage = 0;
private Question[] QuestionBank = new Question[] {
new Question(R.string.question_australia, true),
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)
};
public static<T> boolean getIndex(T[] a, T target) {
for (int i = 0; i < a.length; i++)
if (target.equals(a[i]))
return true;
return false;
}
private boolean[] QuestionsAnswered = new boolean[QuestionBank.length];
private int CurrentIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate calldd");
setContentView(R.layout.activity_quiz);
if (savedInstanceState != null) {
CurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
QuestionsAnswered = savedInstanceState.getBooleanArray(KEY_INDEX);
}
QuestionTextView = findViewById(R.id.question_text_view);
QuestionTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CurrentIndex = (CurrentIndex + 1) % QuestionBank.length;
updateQuestion();
}
});
TrueButton = findViewById(R.id.true_button);
TrueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(true);
}
});
FalseButton = findViewById(R.id.false_button);
FalseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAnswer(false);
}
});
PreviousButton = findViewById(R.id.prev_button);
PreviousButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CurrentIndex = (CurrentIndex + QuestionBank.length - 1) % QuestionBank.length;
updateQuestion();
}
});
NextButton = findViewById(R.id.next_button);
NextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CurrentIndex = (CurrentIndex + 1) % QuestionBank.length;
updateQuestion();
}
});
updateQuestion();
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart called");
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume called");
}
@Override
public void onPause(){
super.onPause();
Log.d(TAG, "onPause called");
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
Log.i(TAG, "onSaveInstanceState");
savedInstanceState.putInt(KEY_INDEX, CurrentIndex);
savedInstanceState.putBooleanArray(KEY_INDEX, QuestionsAnswered);
savedInstanceState.putInt(KEY_INDEX, CurrentIndex);
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG, "onStop called");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy called");
}
private void updateQuestion() {
int question = QuestionBank[CurrentIndex].getTextResId();
QuestionTextView.setText(question);
TrueButton.setEnabled(!QuestionsAnswered[CurrentIndex]);
FalseButton.setEnabled(!QuestionsAnswered[CurrentIndex]);
}
private void checkAnswer(boolean userPressedTrue) {
int PercentCorrect = 0;
boolean answerIsTrue = QuestionBank[CurrentIndex].isAnswerTrue();
int messageResId = 0;
if (userPressedTrue == answerIsTrue) {
messageResId = R.string.correct_toast;
Percentage++;
} else {
messageResId = R.string.incorrect_toast;
}
Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
QuestionsAnswered[CurrentIndex] = true;
TrueButton.setEnabled(false);
FalseButton.setEnabled(false);
if (getIndex(QuestionBank, QuestionBank[QuestionBank.length - 1])) { //causes app to crash
PercentCorrect = Percentage/QuestionBank.length;
String Correct = "Percent correct: " + PercentCorrect;
Toast.makeText(this, Correct, Toast.LENGTH_SHORT).show();
Percentage = 0;
}
}
}
Why is the app crashing and how to stop this?