问题描述
|
每当我在关闭进度对话框后尝试设置内容视图时,都会出现类似...的错误。
04-13 14:40:38.043:WARN / System.err(801):android.view.ViewRoot $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图。
04-13 14:40:38.073:WARN / InputManagerService(59):窗口已经聚焦,忽略了com.android.internal.view.IInputMethodClient$Stub$Proxy@44dde3f0的聚焦增益
我的代码就像...
ProgressDialog dialog = null; //class variable.
dialog = ProgressDialog.show(Login.this,\"\",\"Logging you in. Please wait...\",true);
new Thread() {
public void run() {
try{
//serIoUs work here!
}
dialog.dismiss();
setContentView(R.layout.blaa);
} catch (Exception e) {
e.printstacktrace();
}
}
}.start();
是什么原因造成的?
解决方法
您的线程不是UI线程。我认为您需要创建一个处理程序,然后在需要切换内容视图时向其发布一个Runnable。
Handler hdl = new Handler(); // Will ron on UI-thread
dialog = ProgressDialog.show(Login.this,\"\",\"Logging you in. Please wait...\",true);
new Thread() {
public void run() {
try{
//serious work here!
}
hdl.post(new Runnable() {
public void run() {
dialog.dismiss();
setContentView(R.layout.blaa);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
,您可以这样使用它:
//Initialise
private ProgressDialog m_ProgressDialog = null;
private Runnable myRunnable;
//Make a runnable
myRunnable= new Runnable(){
@Override
public void run(){
Thread.sleep(5000);
setContentView(R.layout.blaa);
m_ProgressDialog.dismiss();
}
};
//initialize your thread
Thread thread = new Thread(null,myRunnable,\"MagentoBackground\");
thread.start();
//open up you dialog
m_ProgressDialog = ProgressDialog.show(SelectStation.this,\"Loading,please wait...\",true);