内螺纹无法创建处理程序,并没有所谓的活套。prepare()里面的AsyncTask的ProgressDialog [英] Can't create handler inside thread that has not called Looper.prepare() inside AsyncTask for ProgressDialog

查看:121
本文介绍了内螺纹无法创建处理程序,并没有所谓的活套。prepare()里面的AsyncTask的ProgressDialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么我得到这个错误。我使用的AsyncTask运行在后台的一些过程。

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

我有:

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...");
}

当我进入 doInBackground()根据条件我:

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

每当我尝试 downloadSpinnerProgressDialog.show()我收到错误。

任何想法家伙?

推荐答案

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

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.

您必须调用显示()无论是在 onProgressUpdate() onPostExecute ()

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

例如:

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

这篇关于内螺纹无法创建处理程序,并没有所谓的活套。prepare()里面的AsyncTask的ProgressDialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆