RuntimeException:无法在未调用 Looper.prepare() 的线程内创建处理程序

发布于 2024-12-22 02:54:22 字数 1800 浏览 1 评论 0原文

我有一个带有 ASyncTask 的代码,问题是当我执行它几次时,它会因以下异常而崩溃: RuntimeException: Only one Looper may be create per thread

但后来我读了这个: https://stackoverflow.com/a/7781280/869180 我记得我有一个类似的错误在过去,它与 ASyncTask 中创建的 UI 内容(在我的例子中是 ProgressDialog)有关。

所以我从 ASyncTask 中删除了所有 UI 内容,并且也删除了 Looper.prepare,以避免 RuntimeException,但我知道我得到了这个:

12-21 00:34:17.363: W/System.err(18658): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
12-21 00:34:17.371: W/System.err(18658):    at android.os.Handler.<init>(Handler.java:121)
12-21 00:34:17.371: W/System.err(18658):    at android.app.Activity.<init>(Activity.java:683)
12-21 00:34:17.371: W/System.err(18658):    at com.konex.Alaves.Parser.<init>(Parser.java:29)
12-21 00:34:17.371: W/System.err(18658):    at com.konex.Alaves.News$LoadNews.doInBackground(News.java:131)

这是代码:

private class LoadNews extends AsyncTask<String, Void, Void> 
{
    private List<Noticia> data = new ArrayList<Noticia>();

    @Override
    protected void onPreExecute() {
        m_dialog.show();
    }

    @Override
    protected Void doInBackground(String... url) {
        try {

//          Looper.myLooper();
//          Looper.prepare();
            Parser parser = new Parser(url[0], url[1]);
            data = parser.run();

           } catch (Exception e) { 
               e.printStackTrace();
           }
        return null;
    }

@Override
    protected void onPostExecute(Void result) {

            m_dialog.dismiss();

        if(data !=null )                
            showNewContent(data);
    }
}

我确定我错过了一些东西或者我正在做有些不好的东西,但我无法在任何地方找到它。

多谢

I've got a code with an ASyncTask and the problem is that when I execute it several times it crashes with this exception: RuntimeException: Only one Looper may be created per thread

But then I've read this: https://stackoverflow.com/a/7781280/869180 and I remembered that I had a similar error in the past and it was related to the UI stuff (a ProgressDialog in my case) created in the ASyncTask.

So I took off all the UI stuff from the ASyncTask and I removed Looper.prepare too, to avoid that RuntimeException, but know I'm getting this:

12-21 00:34:17.363: W/System.err(18658): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
12-21 00:34:17.371: W/System.err(18658):    at android.os.Handler.<init>(Handler.java:121)
12-21 00:34:17.371: W/System.err(18658):    at android.app.Activity.<init>(Activity.java:683)
12-21 00:34:17.371: W/System.err(18658):    at com.konex.Alaves.Parser.<init>(Parser.java:29)
12-21 00:34:17.371: W/System.err(18658):    at com.konex.Alaves.News$LoadNews.doInBackground(News.java:131)

Here is the code:

private class LoadNews extends AsyncTask<String, Void, Void> 
{
    private List<Noticia> data = new ArrayList<Noticia>();

    @Override
    protected void onPreExecute() {
        m_dialog.show();
    }

    @Override
    protected Void doInBackground(String... url) {
        try {

//          Looper.myLooper();
//          Looper.prepare();
            Parser parser = new Parser(url[0], url[1]);
            data = parser.run();

           } catch (Exception e) { 
               e.printStackTrace();
           }
        return null;
    }

@Override
    protected void onPostExecute(Void result) {

            m_dialog.dismiss();

        if(data !=null )                
            showNewContent(data);
    }
}

I'm sure I'm missing something or I am doing something bad, but I'm not able to find it anywhere.

Thanks a lot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疏忽 2024-12-29 02:54:22

正如堆栈跟踪告诉您的那样,您的问题源于 Parser.java 的第 29 行,位于 Parser 初始值设定项中。您会注意到,这不是您在此处包含的源代码,它是用于 LoadNews 的。

根据堆栈跟踪的前一行,可以是:

  • Parser 继承自 Activity

  • Parser 正在尝试实例化 Activity

这些都不是 可能的。

As the stack trace tells you, your problem originates from line 29 of Parser.java, in the Parser initializers. You will note that this is not the source code you included here, which is for LoadNews.

Based on the preceding line of the stack trace, either:

  • Parser inherits from Activity

  • Parser is trying to instantiate an Activity

Neither of those is possible.

浅忆 2024-12-29 02:54:22

也许在 LoadNews 类之外实例化 Parser 并传递对它的引用?

Maybe instantiate Parser outside LoadNews class and pass reference to it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文