是否检查 运行 的倒计时?

check countdowntime to running or not?

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

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;

所以如果计时器不为空,则意味着它是运行。如果它是 null 意味着用户已经取消了它。