我的 ListActivity 有问题,我使用一个线程来显示一个 ProgressDialog,其中获取所有已安装应用程序的列表。我将其转换为列表适配器,然后我想设置 Activity 的列表适配器,但我无法从线程中执行此操作。
我收到以下错误:ERROR/AndroidRuntime(14429): android.view.ViewRoot$CalledFromWrongThreadException: 只有创建 View 层次结构的原始线程才能触摸其 View 。
progressDialog = new ProgressDialog(this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage("Loading...");
progressDialog.show();
new Thread() {
public void run() {
showList(); // the method where i get all the apps and have the setListAdapter(); method
progressDialog.dismiss();
}
}.start();
如何获取它以便我可以从线程内部使用 setListAdapter
最佳答案
我解决了这个问题,通过使用处理程序,我设法将 setListAdapter 从线程中取出,并仍在等待线程执行该方法。
final Handler h = new Handler(){
public void handleMessage(Message msg) {
setListAdapter(appsAdapter);
}
};
new Thread() {
public void run() {
showList();
progressDialog.dismiss();
h.sendEmptyMessage(0);
}
}.start();
关于android - 从线程内部调用 setListAdapter(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3047999/