Java 安卓测验计时器

Java 安卓测验计时器,java,android,Java,Android,大家好!我正在用定时器制作android问答应用程序。每个问题都有30秒的回答时间。然后当我进入下一个问题时刷新计时器。现在,我在我的应用程序中发现了一个错误,我的测验计时器没有刷新或返回到零,即使我进入下一个问题?有人能帮我吗?非常感谢 公共类QuizHistoryActivity扩展了AppCompatActivity{ private TextView timer; private TextView countLabel; private TextView questionLabel; p

大家好!我正在用定时器制作android问答应用程序。每个问题都有30秒的回答时间。然后当我进入下一个问题时刷新计时器。现在,我在我的应用程序中发现了一个错误,我的测验计时器没有刷新或返回到零,即使我进入下一个问题?有人能帮我吗?非常感谢

公共类QuizHistoryActivity扩展了AppCompatActivity{

private TextView timer;
private TextView countLabel;
private TextView questionLabel;
private Button answerBtn1;
private Button answerBtn2;
private Button answerBtn3;

private String rightAnswer;
private int rightAnswerCount = 0;
private int quizCount = 1;

static final private int QUIZ_COUNT = 10;

ArrayList<ArrayList<String>> quizArray = new ArrayList<>();

String quizData [][] = {
//Question  
{"What is the meaning of ALCU?",
//Answer   
"The association of Local College and University",
//Choice A      
"The association of Local College and University",
//Choice B     
 "The Association of Land Colleges  and Universities",
//Choice C      
"The Association of Level Colleges and Universities"}
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz_history);


    countLabel = (TextView)findViewById(R.id.countlabel);
    questionLabel= (TextView)findViewById(R.id.questionlabel);
    answerBtn1 = (Button)findViewById(R.id.answerbtn1);
    answerBtn2 = (Button)findViewById(R.id.answerbtn2);
    answerBtn3 = (Button)findViewById(R.id.answerbtn3);
    timer = (TextView)findViewById(R.id.timers);

    //Create quizArray from quizdata
    for (int i = 0; i < quizData.length; i++) {

        //Prepare array
        ArrayList<String> tmpArray = new ArrayList<>();
        tmpArray.add(quizData[i][0]);
        tmpArray.add(quizData[i][1]);
        tmpArray.add(quizData[i][2]);
        tmpArray.add(quizData[i][3]);
        tmpArray.add(quizData[i][4]);

        //Add tmpArray to quizArray
        quizArray.add(tmpArray);

    }
    CountDownTimer countDown;
    countDown= new CountDownTimer(30000, 1000) {

        public void onTick(long millisUntilFinished) {
            timer.setText("seconds remaining: " + millisUntilFinished / 1000);
        }

        public void onFinish() {
    showNextQuiz();
        }
    };
    countDown.start();
}

public  void showNextQuiz () {

    //Update quizCountLabel
    countLabel.setText("Question #" + quizCount);

    //Generate random number between 0 and 14 (Quiz Array's size -1)

    Random random = new Random();
    int randomNum = random.nextInt(quizArray.size());

    //Pick ine quiz set
    ArrayList<String> quiz = quizArray.get(randomNum);

    //Set question and right answer
    //array format
    questionLabel.setText(quiz.get(0));
    rightAnswer = quiz.get(1);

    //remove "country" from quiz and shuffle choice
    quiz.remove(0);
    Collections.shuffle(quiz);

    //Set Choices
    answerBtn1.setText(quiz.get(0));
    answerBtn2.setText(quiz.get(1));
    answerBtn3.setText(quiz.get(2));

    //Remove this quiz from quizArray
    quizArray.remove(randomNum);
}

public void checkAnswer (View view) {

    //Get pushed button
    Button answerBtn = (Button)findViewById(view.getId());
    String btnText = answerBtn.getText().toString();

    String alertTitle;

    if(btnText.equals(rightAnswer)) {
        //Correct!
        alertTitle = "Correct!";
        rightAnswerCount++;
    }else {
        //Wrong
        alertTitle = "Wrong";
    }

    //create Dialog
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(alertTitle);
    builder.setMessage("Answer : \n \t \t" + rightAnswer);
    builder.setPositiveButton("Got It!", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            if (quizCount == QUIZ_COUNT) {
                //Show Result

                Intent resultintent = new Intent(getApplicationContext(), ResultQuizHistoryActivity.class);
                resultintent.putExtra("RIGHT_ANSWER_COUNT", rightAnswerCount);
                startActivity(resultintent);
            }else {
                quizCount++;
                showNextQuiz();
            }
        }
    });
    builder.setCancelable(false);
    builder.show();
}
私有文本视图定时器;
私有文本视图标签;
私有文本视图问题标签;
专用按钮应答器BTN1;
专用按钮应答器BTN2;
专用按钮应答器BTN3;
私有字符串右应答;
private int rightAnswerCount=0;
私有整数quizCount=1;
静态最终私人智力测验计数=10;
ArrayList quizArray=新的ArrayList();
字符串quizData[][]{
//问题:
{“ALCU是什么意思?”,
//答复
“地方学院和大学协会”,
//选择A
“地方学院和大学协会”,
//选择B
“土地学院和大学协会”,
//选择C
“一级学院和大学协会”}
};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u quick\u history);
countLabel=(TextView)findViewById(R.id.countLabel);
questionLabel=(TextView)findViewById(R.id.questionLabel);
answerBtn1=(按钮)findViewById(R.id.answerBtn1);
answerBtn2=(按钮)findViewById(R.id.answerBtn2);
answerBtn3=(按钮)findViewById(R.id.answerBtn3);
timer=(TextView)findViewById(R.id.timers);
//从quizdata创建quizArray
for(int i=0;i

}

您正在onCreate方法中创建倒计时,该方法在活动生命周期中调用一次。您应该将其创建为类的实例,并在显示新问题之前尝试重置它。

您在问题中发布了许多代码,这使我们(以及未来的读者)不清楚问题的确切位置。请将问题代码减少到10行或更少。见:和。