无法在未在AsyncTask for ProgressDialog中调用Looper.prepare()的线程内创建处理程序

时间:2022-01-10 20:45:41

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

我不明白为什么我收到这个错误。我正在使用AsyncTask在后台运行某些进程。

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:

当我进入doInBackground()取决于条件I:

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

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

每当我尝试downloadSpinnerProgressDialog.show()时,我都会收到错误。

Any ideas guys?

任何想法的家伙?

4 个解决方案

#1


104  

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.

方法show()必须从用户界面(UI)线程调用,而doInBackground()在不同的线程上运行,这是AsyncTask设计的主要原因。

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

您必须在onProgressUpdate()或onPostExecute()中调用show()。

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

#2


77  

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

我有一个类似的问题,但从阅读这个问题,我想我可以在UI线程上运行:

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

Seems to do the trick for me.

似乎为我做了伎俩。

#3


1  

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

我也很难做这项工作,我的解决方案是使用hyui和konstantin答案,

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

}

#4


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

#1


104  

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.

方法show()必须从用户界面(UI)线程调用,而doInBackground()在不同的线程上运行,这是AsyncTask设计的主要原因。

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

您必须在onProgressUpdate()或onPostExecute()中调用show()。

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

#2


77  

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

我有一个类似的问题,但从阅读这个问题,我想我可以在UI线程上运行:

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

Seems to do the trick for me.

似乎为我做了伎俩。

#3


1  

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

我也很难做这项工作,我的解决方案是使用hyui和konstantin答案,

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

}

#4


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