我有一个工作线程,偶尔我会使用Handler.Post()
. 在某些情况下,我需要工作线程等到Handler.Post()
在 UI 线程上执行并修改视图,在修改UI线程后,通知工作线程继续......这是我的简单工作线程:
workerThread = new Thread() {
@Override
public void run() {
progressBarHandler.post(new Runnable() {
public void run() {
//Step1: which works ok
ActionModeButton.performClick();
}
}
//Step2: returns null pointer exception because ActionMode
//is not yet created and R.id.select_recording is an
//ActionMode button if I put Thread.sleep(1000); here it
//will work fine.
final View selectRecording = getActivity()
.findViewById(R.id.select_recording);
selectRecording.post(new Runnable() {
public void run() {
selectRecording.performClick();
}
});
}
}
workerThread.start();