Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: Progress Dialog error in AysncTask
    primarykey
    data
    text
    <p>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:</p> <pre><code>class ShowDialogAsyncTask extends AsyncTask&lt;Void, String, Void&gt;{ 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(); } } </code></pre> <p>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.</p> <p>I am pretty new to android development so I would very much appreciate any pointers.</p> <p>The Logcat entries where it all goes a bit pear shaped.</p> <pre><code>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 </code></pre> <p><strong>--UPDATE--</strong></p> <p>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</p> <pre><code>progressSpinner = new ProgressDialog(mContext); </code></pre> <p>It would then allow all the other methods of the AsyncTask class access to update the message.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload