2

我正在启动asynctask在 a 内部SherlockListFragment创建的内部 aSherlockFragmentActivity作为选项卡。

我将我的活动上下文传递给asynctask构造函数,并像这样在里面初始化异步任务onCreate()

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

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

private ProgressDialog dialog;
private SherlockFragmentActivity activity;

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

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

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

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

4

2 回答 2

5

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

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.

于 2013-02-27T19:30:50.313 回答
-1

在 Java 中“如果你的方法覆盖了它的超类的方法之一,你可以通过使用关键字super来调用被覆盖的方法。” 因此,在您启动进度对话框时将您的 onPreExecute() 方法更改为:

@Override
protected void onPreExecute(){  
    super.onPreExecute();
    dialog = new ProgressDialog(activity);
    Log.d("AsyncTask!", "Showing dialog now!"); //shown in logcat
    dialog.setMessage("Retreiving all currently airing anime. Please wait.");
    dialog.setCancelable(false);
    dialog.show();  
}
于 2013-02-26T23:10:46.963 回答