简体   繁体   English

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

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

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()取决于条件我:

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

Whenever I try downloadSpinnerProgressDialog.show() I receive the error. 每当我尝试downloadSpinnerProgressDialog.show()我都会收到错误。

Any ideas guys? 有什么想法吗?

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() ,而doInBackground()在不同的线程上运行,这是AsyncTask设计的主要原因。

You have to call show() either in onProgressUpdate() or in onPostExecute() . 您必须在onProgressUpdate()onPostExecute()调用show() 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();
    }
}

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. 似乎为我做了伎俩。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 AsyncTask 中未调用 Looper.prepare() 的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() in AsyncTask Android 2.3无法在未调用Looper.prepare()(AsyncTask)的线程内创建处理程序 - Android 2.3 Can't create handler inside thread that has not called Looper.prepare() (AsyncTask) Android线程错误-无法在未调用Looper.prepare()的线程内创建处理程序 - Android thread error - Can't create handler inside thread that has not called Looper.prepare() 无法在警告对话框线程中未调用Looper.prepare()的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() on alert dialog thread java.lang.RuntimeException:无法在尚未调用Looper.prepare()的线程内创建处理程序 - java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() java.lang.RuntimeException:无法在尚未调用 Looper.prepare() 的线程内创建处理程序错误 - java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() Error 无法在未调用Looper.prepare()Android的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() Android java.lang.RuntimeException:无法在未在Android中调用Looper.prepare()的线程内创建处理程序 - java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() in Android RxJava无法在未调用Looper.prepare()的线程内创建处理程序 - RxJava Can't create handler inside thread that has not called Looper.prepare() 线程化-无法在未调用Looper.prepare()的线程内创建处理程序 - Threading - Can't create handler inside thread that has not called Looper.prepare()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM