gpt4 book ai didi

java - 检查是否运行倒计时?

转载 作者:太空宇宙 更新时间:2023-11-04 12:33:57 24 4
gpt4 key购买 nike

我有定时器,我可以将定时器设置为响铃。我还有一个暂停按钮来停止计时器。当您第一次访问应用程序时,会导致没有计时器暂停。当您单击暂停按钮时,我收到错误。 这是我的计时器:

long result = date2.getTime() - date1.getTime();

timer = new CountDownTimer(result, 1000) {


public void onTick(long millisUntilFinished) {
time.setText(("seconds remaining: " + millisUntilFinished / 1000));
//create HashMap<Integer,String> textMap at the constructer of the adapter
//now fill this info int'o it
textMap.put(new Integer(position), "seconds remaining: " + millisUntilFinished / 1000);
//notify about the data change
notifyDataSetChanged();

}

public void onFinish() {

time.setVisibility(View.INVISIBLE);
//create HashMap<Integer,String> textMap at the constructer of the adapter
//now fill this info into it

textMap.put(new Integer(position),null);
//notify about the data change
notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "finish", Toast.LENGTH_LONG).show();
if (rowView != lastview || mediaPlayer == null) {
play(position);
if (lastview != null)
lastview = rowView;
} else {
play.setImageResource(R.drawable.n_btn_play_unselected);
mediaPlayer.release();
lastview = null;
}


}

}.start();
deleteDialog.dismiss();
}
});

这是我的暂停按钮:

      stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
time.setVisibility(View.INVISIBLE);
//create HashMap<Integer,String> textMap at the constructer of the adapter
//now fill this info into it
int position=0;
textMap.put(new Integer(position),null);
//notify about the data change
notifyDataSetChanged();

timer.cancel();


} });

如何检查我的时间是否正在运行?点击它时我得到了 nullpointexecption。

最佳答案

检查它是否正在运行:

if(timer != null){
//Here the timer will be running
}

但是,当您取消时,您必须执行以下操作:

timer.cancel();
timer = null;

因此,如果计时器不为空,则表示它正在运行。如果为空则表示用户已取消。

关于java - 检查是否运行倒计时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37532064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com