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

JavaAndroidRuntime ErrorProgressdialogLooper

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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();
    }
}

Solution 2 - Java

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.

Solution 3 - Java

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();
       }
     });
 }

}

Solution 4 - Java

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();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmlevitView Question on Stackoverflow
Solution 1 - JavaKonstantin BurovView Answer on Stackoverflow
Solution 2 - JavahyuiView Answer on Stackoverflow
Solution 3 - Javacaiocpricci2View Answer on Stackoverflow
Solution 4 - Javajayesh kavathiyaView Answer on Stackoverflow