0

编辑:问题解决了。使其全球化。我现在正在做面部护理,伙计们。谢谢!

下面是扩展 AsyncTask 的类的一些片段。我想在 preExecute() 方法中启动一个 progressDialog 并执行一个 progressDialog.dismiss(); 在 postExecute() 方法中。我发现的所有例子都说要像下面那样做。我遇到的问题是对话框超出了 onPostExecute 的范围。这是意料之中的,但所有的例子似乎都是这样做的。我还注意到,在导入处有一个小警告标志,说明导入未使用。这个 ProgressDialog 应该工作吗?我需要传递它吗?

import android.app.ProgressDialog;
...

protected void onPostExecute(Bitmap image){//error when doing this in resetDisplay.... onPostExecute is invoked by the ui thread so this may be why it works here and not in resetDisplay
                    ImageView imageView=(ImageView) parent.findViewById(R.id.imageDisplay);
                    imageView.setImageBitmap(image);

                    dialog.dismiss();

                }
protected void onPreExecute(){
                    ProgressDialog dialog=ProgressDialog.show(parent, "Loading", "Loading the image of the day");
                }
4

3 回答 3

2

请参阅下面的代码

private class ProgressTask extends AsyncTask<String, Void, Boolean> {

    private ProgressDialog dialog = new ProgressDialog(HomeActivity.this);

    /** progress dialog to show user that the backup is processing. */
    /** application context. */

    protected void onPreExecute() {
        this.dialog.setMessage("Please wait");
        this.dialog.show();
    }

    protected Boolean doInBackground(final String... args) {
        try {

            /**
             * Fetch the RSS Feeds from URL
             */
            Utilities.arrayRSS = objRSSFeed
                    .FetchRSSFeeds(Constants.Feed_URL);
            return true;
        } catch (Exception e) {
            Log.e("tag", "error", e);
            return false;
        }
    }

    @Override
    protected void onPostExecute(final Boolean success) {

        if (dialog.isShowing()) {
            dialog.dismiss();
        }

        if (success) {
            // display UI
            UpdateDisplay();
        }
    }
}
于 2012-07-13T10:36:49.833 回答
1
class YourClass extends AsyncTask<...>
{
    ProgressDialog dialog;
    Protected void onPreExecute(){
        // create dialog here
       dialog= new ProgressDialog (...);
    }
    protected void onPostExecute(Bitmap image){
        //
        dialog.dismiss();
    }
}
于 2012-07-13T10:26:14.767 回答
0

您需要在活动ProgressDialog dialog中声明对话框的外部onPreExecute() ,然后在 preExecute() 和 postExecute() 方法中使用。
在活动或异步任务中这样声明

ProgressDialog dialog=new Progress Dialog(YourActivity.this);

然后使用 onPreExecute() 中的对话框作为

 dialog.setMessage("Your Messgae");
 dialog.show();

并在 onPostExecute() 作为

dialog.dismiss();
于 2012-07-13T10:25:35.963 回答