关于Timer的问题.Timer cancel后再重新new一个,还是会抛出异常
的有关信息介绍如下:timer.cancel()之后TimerTask 和Timer 再次初始化就可以了private Timer timer; private TimerTask Runtask;Handler handler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub // 要做的事情 if (m_Auto) { task = new IDCardTask(); task.execute(); } else { if (timer != null) { timer.cancel(); timer = null; bt_AutoReadidcard.setText("自动读卡"); } } super.handleMessage(msg); } }; bt_AutoReadidcard.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { if (m_Auto) { m_Auto = false; } else { m_Auto = true; timer = new Timer(); Runtask = new TimerTask() { @Override public void run() { // TODO Auto-generated method stub Message message = new Message(); message.what = 1; handler.sendMessage(message); } }; timer.schedule(Runtask, 1000, 4000); bt_AutoReadidcard.setText("停止读卡"); } } });