AsyncTask不在对话框中显示ProgressDialog

最后发布: 2011-11-24 11:34:31


问题

我在自定义对话框中使用AsyncTask。 AsyncTask在活动中工作正常,但对话框内部不显示ProgressDialog。 尝试从对话框,显示对话框的活动,应用程序上下文中获取私有上下文。 结果是相同的:屏幕上没有显示ProgressDialog。

private class ClearCache_Task extends AsyncTask<Integer,Integer,Integer>
{
private ProgressDialog progressBar;
private ArrayList<String> m_subfolders;
private final Context m_con;

public ClearCache_Task(Context con, ArrayList<String> subfolders)
{
        m_con = con;
        m_subfolders = subfolders;
    }

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        progressBar = new ProgressDialog(m_con);
        progressBar.setIndeterminate(true);
        progressBar.setCancelable(false);
        progressBar.setMessage("Please wait...");
        progressBar.show();
    }
    @Override
    protected void onPostExecute(Integer result)
    {
        super.onPostExecute(result);
        progressBar.dismiss();
    }
    @Override
    protected Integer doInBackground(Integer... params)
    {
        try
        {
          m_KFileStorage.ClearCache(m_subfolders);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            Log.e(AppTag, e.toString());
        }
        return 0;
    }
android dialog android-asynctask progressdialog
回答

您如何将对话框附加到活动上?

我就是这样的:

活动中:

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DIALOG_PROGRESS:
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setMessage(Application.string(R.string.dialog_message) + "...");
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setCancelable(true);
            mProgressDialog.show();
            return mProgressDialog;
        default:
            return null;
    }
}

在AsyncTask中:

@Override
protected void onPreExecute() {
    super.onPreExecute();
    mActivity.showDialog(DIALOG_PROGRESS);
}

有关Dialog的更多信息,请参见此处: http : //developer.android.com/guide/topics/ui/dialogs.html


回答

很抱歉提出问题。 我在调用旧代码而不是调用asynctask时犯了错误。:)