“ goClicked”函数是按钮“ Go”的onClick函数,但是当我单击按钮“ Go”时它不会执行(我之所以这样说是因为吐司没有出现),直到我评论了循环进入“ goClicked”功能。我只粘贴了两个函数“ goClicked”和“ countdown”的代码,因为它们是更改变量“ counterRunning”的唯一函数。
public void goClicked(View view) {
afterGoPressed();
countDown();
correctCount = 0;
totalCount = 0;
TextView time = (TextView) findViewById(R.id.time);
while (counterRunning) {
int sum = generateQuestion();
pickOption = generateOptions(sum);
}
}
public void countDown() {
counterRunning = true;
final TextView time = (TextView) findViewById(R.id.time);
final Button tryAgain = (Button) findViewById(R.id.tryAgain);
final TextView result = (TextView) findViewById(R.id.result);
final TextView score = (TextView) findViewById(R.id.score);
int secondsLeft = 30;
time.setText(secondsLeft+"");
CountDownTimer countDownTimer = new CountDownTimer(30000,1000) {
@Override
public void onTick(long millisUntilFinished) {
time.setText(millisUntilFinished/1000 + "");
}
@Override
public void onFinish() {
tryAgain.setVisibility(View.VISIBLE);
result.setText("Your score: " + score.getText());
result.setVisibility(View.VISIBLE);
counterRunning = false;
}
}.start();
}
答案 0 :(得分:1)
这是因为您正在使用以下代码进行无限循环:
while (counterRunning) {
int sum = generateQuestion();
pickOption = generateOptions(sum);
}
该代码将阻塞所有其他指令,直到发现counterRunning
在while块内被更改为false
为止。但这从未发生。因此是无限循环。