2015-12-02 84 views
-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(); 
+0

歡迎來到SO。一個錯誤,請告訴我們什麼錯誤信息說,以及它發生在哪一行 – LarsH

+0

請發佈從'LogCat'輸出的錯誤信息 - 它會使調試更簡單 – PPartisan

+0

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) –

回答

0

也許你應該使用try catch語句。如果你運行它再次,你可能會看到一個logcat錯誤,告訴你它是什麼問題。在catch中,你可以做一個Toast說:「對不起,上一個問題不可用」。祝你好運!