每当我在关闭进度对话框后尝试设置内容 View 时,都会收到类似以下错误:
04-13 14:40:38.043: WARN/System.err(801): android.view.ViewRoot$CalledFromWrongThreadException: 只有创建 View 层次结构的原始线程才能触摸其 View 。 04-13 14:40:38.073:警告/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 线程。我认为您需要创建一个 Handler,然后在需要切换内容 View 时向其发送 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();
关于java - 进度对话框阻止内容 View 切换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5650370/