private Handler workerHandler; // instance variable of UIThread
在 onCreate() 方法中
WorkerThread wt = new WorkerThread();
workerHandler = wt.getHandler();
wt.start();
使用下面的 SeekBar 侦听器方法将消息发送到 WorkerThread 的队列
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tv.setText(String.valueOf(progress));
workerHandler.sendMessage(workerHandler.obtainMessage(99,String.valueOf(progress)));
}
工作线程:
private Handler handler;
//getHandler() method
public void run() {
super.run();
Looper.prepare();
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 99)
Log.d("RTR","Message from Main Thread:"+(String)msg.obj);
}
};
Looper.loop();
但是当我更改时,会SeekBar
出现“程序不幸停止”的消息。可能是什么原因?
更新:问题出在工作线程的处理程序上。workerHandler
它仍然是null
。这是为什么?