进度对话框未出现
我有在 AsyncTask
内启动 ProgressDialog
的代码,它看起来像这样:
class RetrieveApps extends AsyncTask<String, Void, List<ApplicationInfo>> {
PackageManager pm;
@Override
protected List<ApplicationInfo> doInBackground(String...params) {
dialog = ProgressDialog.show(Apps.this,
"Retreiving Application list",
"Retrieving list of installed applications", true);
pm = getPackageManager();
return pm.getInstalledApplications(PackageManager.GET_META_DATA);
}
@Override
protected void onPostExecute(List<ApplicationInfo> result) {
for(ApplicationInfo nfo : result){
Drawable icon = nfo.loadIcon(pm);
String name = nfo.loadLabel(pm).toString();
if(name != null && icon != null){
apps.add(new App(name, icon));
}
}
dialog.dismiss();
}
}
我收到一个 RuntimeException
说
无法在未调用 Looper.prepare() 的线程内创建处理程序
它指向调用 ProgressDialog.show()
的行。
I have code that starts a ProgressDialog
inside an AsyncTask
, it looks like this:
class RetrieveApps extends AsyncTask<String, Void, List<ApplicationInfo>> {
PackageManager pm;
@Override
protected List<ApplicationInfo> doInBackground(String...params) {
dialog = ProgressDialog.show(Apps.this,
"Retreiving Application list",
"Retrieving list of installed applications", true);
pm = getPackageManager();
return pm.getInstalledApplications(PackageManager.GET_META_DATA);
}
@Override
protected void onPostExecute(List<ApplicationInfo> result) {
for(ApplicationInfo nfo : result){
Drawable icon = nfo.loadIcon(pm);
String name = nfo.loadLabel(pm).toString();
if(name != null && icon != null){
apps.add(new App(name, icon));
}
}
dialog.dismiss();
}
}
I'm getting a RuntimeException
saying
Can't create handler inside thread that has not called Looper.prepare()
It points at the line where ProgressDialog.show()
was called.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试上面的类,我将对话框创建移到了在 UI 线程上运行的 onPreExecute 方法中,
记住 onPreExecute 、 onPostExecute 和 onProgressUpdate 方法在 UI 线程上运行
doInBackground 方法在非 UI 线程上运行
try the above class , i moved the dialog creation in the onPreExecute method which runs on UI thread
Remember onPreExecute , onPostExecute and onProgressUpdate methods run on UI thread
doInBackground method runs on non-UI thread
您的对话框创建应该在 UI 线程上进行,以便我可以将进度发回对话框,并使用您的对话框创建代码实现
onPreExecute()
。要发布进度调用并实现onProgressUpdate (Progress...values)
。Your dialog creation is suppose to be on the UI thread so that I can post back progress to the dialog, implement
onPreExecute()
with your dialog creation code. To post progress call and implementonProgressUpdate (Progress... values)
.将其
移至 onPreExecute 方法。
onPreExecute(以及 onPostExecute,您将在其中取消对话框)发生在 UI 线程上,因此可以进行 UI 更改。
doOnBackground 在不同的线程中工作,因此无法更改 UI。
Move this:
To the onPreExecute method.
onPreExecute (and onPostExecute, where you will cancel the dialog) happens on the UI thread and therefore can make UI changes.
doOnBackground works in a different thread and therefore can't make UI changes.