RuntimeException:无法在未调用 Looper.prepare() 的线程内创建处理程序
我有一个带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如堆栈跟踪告诉您的那样,您的问题源于
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 theParser
initializers. You will note that this is not the source code you included here, which is forLoadNews
.Based on the preceding line of the stack trace, either:
Parser
inherits fromActivity
Parser
is trying to instantiate anActivity
Neither of those is possible.
也许在 LoadNews 类之外实例化 Parser 并传递对它的引用?
Maybe instantiate Parser outside LoadNews class and pass reference to it?