这是我的代码,
public ProgressDialog loadingdialog;
public void ShowManager() {
//do something
}
public void startScan() {
loadingdialog = ProgressDialog.show(WifiManagementActivity.this,
"","Scanning Please Wait",true);
new Thread() {
public void run() {
try {
sleep(4000);
ShowManager();
} catch(Exception e) {
Log.e("threadmessage",e.getMessage());
}
loadingdialog.dismiss();
}
}.start();
}
startScan();
一个基本的progressdialog show函数,但是在调用ShowManager()的行上,得到错误,
01-07 23:11:36.081: ERROR/threadmessage(576): Only the original thread
that created a view hierarchy can touch its views.
编辑:
ShowManager()是一个更改视图元素的函数.不久之后,
public void ShowManager()
{
TextView mainText = (TextView) findViewById(R.id.wifiText);
mainText.setText("editted");
}
解决方法:
我找到了答案.我不想回答我自己的问题,但也许this会帮助别人.在单独的线程中,我们无法更新大多数UI对象.我们必须创建一个处理程序并更新其中的视图.
public ProgressDialog loadingdialog;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
loadingdialog.dismiss();
ShowManager();
}
};
public void ShowManager()
{
TextView mainText = (TextView) findViewById(R.id.wifiText);
mainText.setText("editted");
}
public void startScan() {
loadingdialog = ProgressDialog.show(WifiManagementActivity.this,
"","Scanning Please Wait",true);
new Thread() {
public void run() {
try {
sleep(4000);
handler.sendEmptyMessage(0);
} catch(Exception e) {
Log.e("threadmessage",e.getMessage());
}
}
}.start();
}
startScan();