开发者

ProgressDialog in AsyncTask not updating or dismissing

I am using an AsyncTask to handle complex background operations (compiling a log file to send) and I use a ProgressDialog to show the user progress. I have tried using showDialog() but it never seems to show or dismiss (it is never called), and I followed tutorials on how to do it... So I am using unmanaged ones, and it won't dismiss my message. I am also wanting to update the message as it starts to compile the log file (as it seems to lag there - or maybe the text view is just really long so it doesn't update like it is supposed to).

I have moved my code around a bit so it look like there are problems (like onProgressUpdate()), but I don't know how to make it work. I have looked around this site and nothing seems to be having the problem I am (not exactly anyways). RunOnUiThread() doesn't work, new Thread(){} doesn't work, and onProgressUpdate() I can't get to work (the documentation is confusing on this).

It also never dismisses. I have set up a listener and it never dismisses.

Does anyone know what is wrong with my code? I thought AsyncTask was supposed to be simple.

        private class BuildLogTask extends AsyncTask<Void, Void, String> {
        String temp;
        ProgressDialog progressdialog = new ProgressDialog(context); //variable is defined at onCreate (held as private, not static)

        @Override
        protected String doInBackground(Void... params) {
            temp = buildLog();
            logdata = temp;
            publishProgress();
            createLogFile();
            return temp;
        }

        protected void onProgressUpdate() {
            progressdialog.setMessage("Compiling Log File...");
        }

        @Override
        protected void onPreExecute() {
            Log.w(TAG,"Showing Dialog");
            send.setEnabled(开发者_如何转开发false);
            ProgressDialog progressdialog = new ProgressDialog(context);
            progressdialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressdialog.setMessage("Gathering Data...");
            progressdialog.setCancelable(false);
            progressdialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

                @Override
                public void onDismiss(DialogInterface dialog) {
                    Log.e(TAG,"Progress Dialog dismissal.");

                }
            });
            progressdialog.show();
        }

        @Override
        protected void onCancelled(){
            Log.e(TAG,"Progress Dialog was Cancelled");
            progressdialog.dismiss();
            logdata=null;
        }

        @Override
        protected void onPostExecute(String result) {
            progressdialog.dismiss();
            send.setEnabled(true);
            previewAndSend();
        }

    }


You have two different progress dialogs there, one local to onPreExecute() and one global. The one ur dismissing in your onPostExecution() is your global one which was never actually shown. Remove the local declaration and it should work.


There are two problems.

  1. The signature for on onProgressUpdate is not correct. Try this instead:

    @Override protected void onProgressUpdate(Void... progress) { progressdialog.setMessage("Compiling Log File..."); }

  2. You're masking the progressDialog member variable with a local variable in onPreExecute()

EDIT: Second problem identified:


Try it,

Replace:

progressdialog.show();

For:

progressdialog = progressdialog.show();

Good luck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜