进度对话框未出现

发布于 2024-12-02 12:01:55 字数 1144 浏览 3 评论 0原文

我有在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苦行僧 2024-12-09 12:01:55
public class RetrieveApps extends AsyncTask<String, Void, List<ApplicationInfo>> {
    PackageManager pm;

    @Override
    protected List<ApplicationInfo> doInBackground(String...params) {

        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();
    }

    @Override
    protected void onPreExecute(){
      dialog = ProgressDialog.show(Apps.this,
                "Retreiving Application list",
                "Retrieving list of installed applications", true);

    }

}

尝试上面的类,我将对话框创建移到了在 UI 线程上运行的 onPreExecute 方法中,

记住 onPreExecute 、 onPostExecute 和 onProgressUpdate 方法在 UI 线程上运行
doInBackground 方法在非 UI 线程上运行

public class RetrieveApps extends AsyncTask<String, Void, List<ApplicationInfo>> {
    PackageManager pm;

    @Override
    protected List<ApplicationInfo> doInBackground(String...params) {

        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();
    }

    @Override
    protected void onPreExecute(){
      dialog = ProgressDialog.show(Apps.this,
                "Retreiving Application list",
                "Retrieving list of installed applications", true);

    }

}

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

凉风有信 2024-12-09 12:01:55

您的对话框创建应该在 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 implement onProgressUpdate (Progress... values).

刘备忘录 2024-12-09 12:01:55

将其

dialog = ProgressDialog.show(Apps.this,
        "Retreiving Application list",
        "Retrieving list of installed applications", true);

移至 onPreExecute 方法。

onPreExecute(以及 onPostExecute,您将在其中取消对话框)发生在 UI 线程上,因此可以进行 UI 更改。
doOnBackground 在不同的线程中工作,因此无法更改 UI。

Move this:

dialog = ProgressDialog.show(Apps.this,
        "Retreiving Application list",
        "Retrieving list of installed applications", true);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文