我使用进度dialog.i需要停止线程,当用户关闭progressdialog。不幸的是它给人的异常请帮助我..java.lang.IllegalMonitorStateException:对象在wait()之前未被线程锁定?
在内部类
class UpdateThread extends Thread{
public void run() {
while (true){
count=adapter.getCount();
try {
mHandler.post( new Runnable() {
public void run() {
Log.i(TAG,count+"count");
progressDialog.setMessage(count + "Device found");
}
});
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
的onCreate
updateThread=new UpdateThread();
progressDialog= new ProgressDialog(GroupListActivity.this);
synchronized (this) {
updateThread.start();
}
ondismissal
progressDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
try {
synchronized (this) {
updateThread.wait(300);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i(TAG,"Thread is stopped");
}
});
你究竟想用锁来达到什么目的? – Krease 2014-10-27 17:14:53
@Chris它的异常。因此我必须锁定 – Asthme 2014-10-27 17:20:12
我明白,如果您要调用'wait',则必须使用synchronized,但是我没有得到(a)为什么要同步调用updateThread。开始“,或(b)为什么你使用'wait'开始(因为你没有使用'notify'或'notifyAll')。我怀疑你正在同步并在不应该使用“wait”的地方使用。 – Krease 2014-10-27 17:25:45