Can't create handler inside thread that has not called Looper.prepare() inside AsyncTask for ProgressDialog
Asked Answered
S

4

78

I don't understand why I'm getting this error. I'm using AsyncTask to run some processes in the background.

I have:

protected void onPreExecute() 
{
    connectionProgressDialog = new ProgressDialog(SetPreference.this);
    connectionProgressDialog.setCancelable(true);
    connectionProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    connectionProgressDialog.setMessage("Connecting to site...");
    connectionProgressDialog.show();

    downloadSpinnerProgressDialog = new ProgressDialog(SetPreference.this);
    downloadSpinnerProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    downloadSpinnerProgressDialog.setMessage("Downloading wallpaper...");
}

When I get into doInBackground() depending on a condition I:

[...]    
connectionProgressDialog.dismiss();
downloadSpinnerProgressDialog.show();
[...]

Whenever I try downloadSpinnerProgressDialog.show() I receive the error.

Any ideas guys?

Springhead answered 1/9, 2010 at 2:32 Comment(0)
P
106

The method show() must be called from the User-Interface (UI) thread, while doInBackground() runs on different thread which is the main reason why AsyncTask was designed.

You have to call show() either in onProgressUpdate() or in onPostExecute().

For example:

class ExampleTask extends AsyncTask<String, String, String> {

    // Your onPreExecute method.

    @Override
    protected String doInBackground(String... params) {
        // Your code.
        if (condition_is_true) {
            this.publishProgress("Show the dialog");
        }
        return "Result";
    }

    @Override
    protected void onProgressUpdate(String... values) {
        super.onProgressUpdate(values);
        connectionProgressDialog.dismiss();
        downloadSpinnerProgressDialog.show();
    }
}
Pile answered 1/9, 2010 at 3:16 Comment(2)
That didn't do the trick for me, just tried "AlertDialog.Builder builder = new AlertDialog.Builder(context);" and added the configuration of the builder. Trying to show this inside the onProgressUpdate leads to exactly the same error. Any ideas ?Riedel
Thanks Konstantin, I didn't realise you could override onProgressUpdate with any variable type using publishProgress(). Really helped me, to simply update with toast messages.Moorer
M
81

I had a similar issue but from reading this question I figured I could run on UI thread:

YourActivity.this.runOnUiThread(new Runnable() {
    public void run() {
        alertDialog.show();
    }
});

Seems to do the trick for me.

Multiflorous answered 1/9, 2011 at 12:5 Comment(1)
getActivity().runOnUiThread(this::mymethod); Thanks worked very well.Floaty
S
1

I had a hard time making this work too, the solution for me was to use both hyui and konstantin answers,

class ExampleTask extends AsyncTask<String, String, String> {

// Your onPreExecute method.

@Override
protected String doInBackground(String... params) {
    // Your code.
    if (condition_is_true) {
        this.publishProgress("Show the dialog");
    }
    return "Result";
}

@Override
protected void onProgressUpdate(String... values) {

    super.onProgressUpdate(values);
    YourActivity.this.runOnUiThread(new Runnable() {
       public void run() {
           alertDialog.show();
       }
     });
 }

}
Slighting answered 16/5, 2012 at 10:8 Comment(1)
I thought stuff done in onProgressUpdate() was automatically going to run on the UI threadLunkhead
A
0
final Handler handler = new Handler() {
        @Override
        public void handleMessage(final Message msgs) {
        //write your code hear which give error
        }
        }

new Thread(new Runnable() {
    @Override
    public void run() {
    handler.sendEmptyMessage(1);
        //this will call handleMessage function and hendal all error
    }
    }).start();
Aurelio answered 28/3, 2012 at 12:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.