Android:AsyncTask 中的进度对话框未显示 [英] Android: Progress Dialog in AsyncTask not showing up

查看:20
本文介绍了Android:AsyncTask 中的进度对话框未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 SherlockListFragment 中启动 asynctask,它是在 SherlockFragmentActivity 内创建的一个选项卡.

I am starting the asynctask inside a SherlockListFragment which was created inside a SherlockFragmentActivity as a tab.

我将 asynctask 构造函数传递给我的 Activity 上下文并在 onCreate() 中像这样初始化 asynctask:

I pass the asynctask constructor my activity context and initialize the asynctask like this inside onCreate():

AsyncTask<String, Integer, String[]> asynctask = new DownloadFilesTask(getSherlockActivity()).execute(url);

AsyncTask 类 DownloadFilesTask 中的构造函数如下所示:

The constructor inside the AsyncTask class DownloadFilesTask looks like this:

private ProgressDialog dialog;
private SherlockFragmentActivity activity;

public DownloadFilesTask(SherlockFragmentActivity activity) {
        this.activity = activity;
        this.dialog = new ProgressDialog(activity);
    }

预执行和后执行如下所示:

Pre-execute and post execute look like this:

protected void onPreExecute(){  
        Log.d("AsyncTask!", "Showing dialog now!"); //shown in logcat
        dialog.setMessage("Retrieving all currently airing anime. Please wait.");
        dialog.setCancelable(false);
        dialog.show();  
    }

.

protected void onPostExecute(String[] result) { 
    Log.d("AsyncTask!", "Dismissing dialog now!"); //shown in logcat
    dialog.dismiss();
}

但是在完成所有后台工作时,进度对话框没有出现!我在这里做错了什么?我认为这可能是上下文问题.

But the progress dialog doesn't show up while all the background work is being done! What am I doing wrong here? I think it might be a context problem.

推荐答案

由于 Mike Repass 关于传递普通旧上下文的评论,部分问题已得到解决.

Part of the problem was fixed thanks to the comment from Mike Repass about passing a plain old context.

至于对话框没有显示......我只是愚蠢,因为我在执行外部 AsyncTask 之后调用了 .get() 阻塞了 UI 线程.显然,对话框不会以这种方式显示.

As for the dialog not showing up...I was just being stupid because I called a .get() after the execute OUTSIDE the AsyncTask which blocks the UI thread. Obviously the dialog is not going to show up that way.

这篇关于Android:AsyncTask 中的进度对话框未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆