0

I have some code that does some processing in an AsyncTask, while this is running I would like to display a Progress Dialog. At various points I was hoping to update the message but I'm having some trouble. Heres what I have so far:

class ShowDialogAsyncTask extends AsyncTask<Void, String, Void>{

    private ProgressDialog progressSpinner;

        @Override
        protected void onPreExecute() {
        ProgressDialog progressSpinner = new ProgressDialog(MainActivity.this);
        progressSpinner.setTitle("File Download");
        progressSpinner.setMessage("Connecting to Internet");
        progressSpinner.show();
        }

        @Override
        protected Void doInBackground(Void... params) 
        {
            Log.v("doInBackground","1");
            try {
                Log.v("doInBackground","2");
                onProgressUpdate("Downloading File");
                //do some stuff
            } catch (IOException e) {
                result = "Error";
            }

            onProgressUpdate("Complete!");
            SystemClock.sleep(300);
            return null;
        }

        protected void onProgressUpdate(String... values) 
        {
        Log.e("onProgressUpdate",values[0]);
        try{
        progressSpinner.setMessage(values[0]);
        }catch(Exception e){Log.e("onProgressUpdate","Error!");}
        Log.e("onProgressUpdate","Success!");
        }

        @Override
        protected void onPostExecute(Void result) 
        {
        progressSpinner.dismiss();
        vf.showNext();
        }
}

The code which I was hoping would have updated the message doesn't work, it just throws an exception. Then the whole thing crashes when I attempt to dismiss the the progress dialog.

I am pretty new to android development so I would very much appreciate any pointers.

The Logcat entries where it all goes a bit pear shaped.

03-22 22:59:30.189: W/dalvikvm(8133): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
03-22 22:59:30.189: E/AndroidRuntime(8133): Uncaught handler: thread main exiting due to uncaught exception
03-22 22:59:30.195: E/AndroidRuntime(8133): java.lang.NullPointerException

--UPDATE--

It appears that my issue was where I had been declaring the progress dialog box. I eventually figured out that I could declare the progress dialog in a newly created constructor like so

progressSpinner = new ProgressDialog(mContext);

It would then allow all the other methods of the AsyncTask class access to update the message.

cosmicsafari
  • 3,949
  • 11
  • 37
  • 56

1 Answers1

2

the problem is probably how you refer to your MainActivity while creating the ProgressDialog. Add a public constructor to your AsyncTask and a class field named mContext:

        public ShowDialogAsyncTask(Context c){
           mContext = c;
        }

Pass this in your MainActivity while creating an object of your class:

         ShowDialogAsyncTask task = new ShowDialogAsyncTask(this);

in your AsyncTask, create the ProgressDialog using that context

Droidman
  • 11,485
  • 17
  • 93
  • 141
  • please post some more LogCat output if possible – Droidman Mar 22 '13 at 23:50
  • it's difficult to tell what's wrong here without having the complete code, but you can take a look on how I used an AsyncTask to download an image and show the progress http://stackoverflow.com/questions/15549421/how-to-download-and-save-an-image-in-android – Droidman Mar 23 '13 at 00:00
  • Using your suggestions I got there in the end. Thanks. – cosmicsafari Mar 23 '13 at 00:24