我的Android应用在达到countdown
0
时崩溃了。以下是与之相关的代码部分。
final CountDownTimer countdown=new CountDownTimer(60000, 1000){
public void onTick(long millisUntilFinished)
{
tvTime.setText((millisUntilFinished / 1000)+"'s");
}
public void onFinish()
{
try{
tvTime.setText("Time Over");
this.cancel();
Toast.makeText(getApplicationContext(),"Answer: "+ OriginalWord, Toast.LENGTH_LONG).show();
Intent i=new Intent(LastJumble.this,ScoreCard.class);
i.putExtra("username",username);
i.putExtra("totalQues", totalQues);
i.putExtra("count", count);
startActivity(i);
}
catch (Exception e) {
// TODO: handle exception
}
}
}.start();
答案 0 :(得分:3)
this.cancel()
将尝试取消CountDownTimer。由于计时器结束,这将打破。如果您要在外部类中调用cancel方法,则应将其引用为OuterClass.this.cancel()
,其中OuterClass是类的名称。