-1
第一個問題是Question_oceans看到下面的代碼。試圖讓用戶注意,它不能獲得「Prevoius」當按鈕被按下,因爲它是第一個問題的問題」即時得到一個錯誤。安卓後退按鈕錯誤
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();
歡迎來到SO。一個錯誤,請告訴我們什麼錯誤信息說,以及它發生在哪一行 – LarsH
請發佈從'LogCat'輸出的錯誤信息 - 它會使調試更簡單 – PPartisan
AndroidRuntime:致命例外:主 進程:com.bignerdranch.android.geoquiz, android.content.res.Resources $ NotFoundException:字符串資源ID#0x0 atandroid.content.res.Resources.getText(Resources.java:1334) atandroid.widget.Toast .makeText(Toast.java:379) –