安卓:进度对话框中的AsyncTask没有显示出来 [英] Android: Progress Dialog in AsyncTask not showing up
问题描述
我开始了的AsyncTask
在 SherlockListFragment
这是内部创建的 SherlockFragmentActivity
作为标签。
I am starting the asynctask
inside a SherlockListFragment
which was created inside a SherlockFragmentActivity
as a tab.
我通过了的AsyncTask
构造我的活动内容和初始化像这样的AsyncTask里面的onCreate()
:
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-执行和后期执行是这样的:
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.
推荐答案
问题的部分原因是由于固定从迈克再经过大约传递一个普通的旧环境的注释。
Part of the problem was fixed thanks to the comment from Mike Repass about passing a plain old context.
至于对话框显示不出来...我只是愚蠢,因为我叫获得()
后执行外的AsyncTask的哪些块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.
这篇关于安卓:进度对话框中的AsyncTask没有显示出来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!