关于Timer的问题.Timer cancel后再重新new一个,还是会抛出异常


关于Timer的问题.Timer cancel后再重新new一个,还是会抛出异常

文章插图
timer.cancel()之后TimerTask 和Timer 再次初始化就可以了private Timer timer; private TimerTask Runtask;Handler handler = new Handler() {@Overridepublic 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() {@Overridepublic void onClick(View arg0) {if (m_Auto) {m_Auto = false;} else {m_Auto = true;timer = new Timer();Runtask = new TimerTask() {@Overridepublic void run() {// TODO Auto-generated method stubMessage message = new Message();message.what = 1;handler.sendMessage(message);}};timer.schedule(Runtask, 1000, 4000);bt_AutoReadidcard.setText("停止读卡");}}});
【关于Timer的问题.Timer cancel后再重新new一个,还是会抛出异常】

    推荐阅读