无法在未调用 Looper.prepare() 的线程内创建处理程序
我有一个活动,其中我有一个课程。
text=new Dynamictext(...);
text.setText("txt");
在我的 DynamicText java 中,我有以下代码:
public void setText(String text) {
this.text=text;
new asyncCreateText().execute();
//this.createText(text);
}
//private Handler handler = new Handler();
private class asyncCreateText extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... unused) {
return null;
}
@Override
protected void onPostExecute(Void unused) {
}
}
我得到:
错误/AndroidRuntime(5176):原因:java.lang.RuntimeException:无法在未调用 Looper.prepare() 的线程内创建处理程序
我该如何处理此错误?
ERROR/AndroidRuntime(5370): java.lang.ExceptionInInitializerError
ERROR/AndroidRuntime(5370): at com.l.start.DynamicText.setText(DynamicText.java:125)
ERROR/AndroidRuntime(5370): at com.l.start.OpenGLRenderer.initfonts(OpenGLRenderer.java:168)
ERROR/AndroidRuntime(5370): at com.l.start.OpenGLRenderer.init(OpenGLRenderer.java:119)
ERROR/AndroidRuntime(5370): at com.l.start.OpenGLRenderer.onSurfaceChanged(OpenGLRenderer.java:90)
ERROR/AndroidRuntime(5370): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1120)
ERROR/AndroidRuntime(5370): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:975)
ERROR/AndroidRuntime(5370): Caused by: java.lang.RuntimeException:
Can't create handler inside thread that has not called Looper.prepare()
ERROR/AndroidRuntime(5370): at android.os.Handler.<init>(Handler.java:121)
ERROR/AndroidRuntime(5370): at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
ERROR/AndroidRuntime(5370): at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
ERROR/AndroidRuntime(5370): at android.os.AsyncTask.<clinit>(AsyncTask.java:152)
ERROR/AndroidRuntime(5370): ... 6 more
I have an Activity, and in that I have a class.
text=new Dynamictext(...);
text.setText("txt");
in my DynamicText java I have this code:
public void setText(String text) {
this.text=text;
new asyncCreateText().execute();
//this.createText(text);
}
//private Handler handler = new Handler();
private class asyncCreateText extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... unused) {
return null;
}
@Override
protected void onPostExecute(Void unused) {
}
}
I get:
ERROR/AndroidRuntime(5176): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
How can I handle this error?
ERROR/AndroidRuntime(5370): java.lang.ExceptionInInitializerError
ERROR/AndroidRuntime(5370): at com.l.start.DynamicText.setText(DynamicText.java:125)
ERROR/AndroidRuntime(5370): at com.l.start.OpenGLRenderer.initfonts(OpenGLRenderer.java:168)
ERROR/AndroidRuntime(5370): at com.l.start.OpenGLRenderer.init(OpenGLRenderer.java:119)
ERROR/AndroidRuntime(5370): at com.l.start.OpenGLRenderer.onSurfaceChanged(OpenGLRenderer.java:90)
ERROR/AndroidRuntime(5370): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1120)
ERROR/AndroidRuntime(5370): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:975)
ERROR/AndroidRuntime(5370): Caused by: java.lang.RuntimeException:
Can't create handler inside thread that has not called Looper.prepare()
ERROR/AndroidRuntime(5370): at android.os.Handler.<init>(Handler.java:121)
ERROR/AndroidRuntime(5370): at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
ERROR/AndroidRuntime(5370): at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
ERROR/AndroidRuntime(5370): at android.os.AsyncTask.<clinit>(AsyncTask.java:152)
ERROR/AndroidRuntime(5370): ... 6 more
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
该错误是不言自明的...
doInBackground()
在后台线程上运行,由于它不打算循环,因此未连接到Looper
。您很可能根本不想直接实例化处理程序...您的
doInBackground()
实现返回的任何数据都将传递给在用户界面线程。在出现问题的 STACKTRACE 之后添加:
看起来您正在尝试从 GL 渲染线程启动
AsyncTask
...不要这样做,因为他们不会也曾经Looper.loop()
。 AsyncTasks 实际上被设计为仅从 UI 线程运行。破坏性最小的修复可能是使用启动
AsyncTask
的Runnable
调用Activity.runOnUiThread()
。The error is self-explanatory...
doInBackground()
runs on a background thread which, since it is not intended to loop, is not connected to aLooper
.You most likely don't want to directly instantiate a Handler at all... whatever data your
doInBackground()
implementation returns will be passed toonPostExecute()
which runs on the UI thread.ADDED FOLLOWING THE STACKTRACE APPEARING IN QUESTION:
Looks like you're trying to start an
AsyncTask
from a GL rendering thread... don't do that cos they won't everLooper.loop()
either. AsyncTasks are really designed to be run from the UI thread only.The least disruptive fix would probably be to call
Activity.runOnUiThread()
with aRunnable
that kicks off yourAsyncTask
.上面的所有答案都是正确的,但我认为这是最简单的示例:
它的作用是从通过活动中声明的处理程序的 post() 方法传递的完全不同的线程更新 UI 线程中的进度条。
希望有帮助!
All the answers above are correct, but I think this is the easiest example possible:
What this does is update a progress bar in the UI thread from a completely different thread passed through the post() method of the handler declared in the activity.
Hope it helps!
您可以通过这种方式在后台线程中创建处理程序
You create handler in background thread this way
Activity.runOnUiThread()
对我不起作用。我通过以下方式创建一个常规线程来解决这个问题:并以这种方式从 GL 更新中调用它:
Activity.runOnUiThread()
does not work for me. I worked around this issue by creating a regular thread this way:and calling it from the GL update this way:
尝试从 UI 线程运行异步任务。当我没有做同样的事情时,我遇到了这个问题!
Try running you asyntask from the UI thread. I faced this issue when I wasn't doing the same!
试试这个
Try this
对于所有 Kotlin 爱好者,这里是适合您的代码
For all the Kotlin lovers, here is the code for you