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:
在我的动态文本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) {
}
}
I get:
我得到:
ERROR/AndroidRuntime(5176): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
造成的错误/ AndroidRuntime(5176)::. lang。RuntimeException:不能在未调用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
6 个解决方案
#1
142
The error is self-explanatory... doInBackground()
runs on a background thread which, since it is not intended to loop, is not connected to a Looper
.
这个错误是不言而喻的…doInBackground()运行在一个后台线程上,由于它不打算循环,所以没有连接到一个循环。
You most likely don't want to directly instantiate a Handler at all... whatever data your doInBackground()
implementation returns will be passed to onPostExecute()
which runs on the UI thread.
您很可能不想直接实例化一个处理程序……无论doInBackground()实现返回什么数据,都将传递给运行在UI线程上的onPostExecute()。
mActivity = ThisActivity.this;
mActivity.runOnUiThread(new Runnable() {
public void run() {
new asyncCreateText().execute();
}
});
ADDED FOLLOWING THE STACKTRACE APPEARING IN QUESTION:
添加以下出现问题的STACKTRACE:
Looks like you're trying to start an AsyncTask
from a GL rendering thread... don't do that cos they won't ever Looper.loop()
either. AsyncTasks are really designed to be run from the UI thread only.
看起来您正在尝试从一个GL呈现线程启动一个AsyncTask……不要这样做,因为它们也不会loop .loop()。AsyncTasks实际上是设计为只从UI线程运行。
The least disruptive fix would probably be to call Activity.runOnUiThread()
with a Runnable
that kicks off your AsyncTask
.
破坏性最小的解决方案可能是调用Activity.runOnUiThread(),并使用一个Runnable来启动异步任务。
#2
28
All the answers above are correct, but I think this is the easiest example possible:
上面所有的答案都是正确的,但我认为这是最简单的例子:
public class ExampleActivity extends Activity {
private Handler handler;
private ProgressBar progress;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progress = (ProgressBar) findViewById(R.id.progressBar1);
handler = new Handler();
}
public void clickAButton(View view) {
// Do something that takes a while
Runnable runnable = new Runnable() {
@Override
public void run() {
handler.post(new Runnable() { // This thread runs in the UI
@Override
public void run() {
progress.setProgress("anything"); // Update the UI
}
});
}
};
new Thread(runnable).start();
}
}
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.
它所做的是,从通过活动中声明的处理程序的post()方法传递的完全不同的线程更新UI线程中的进度条。
Hope it helps!
希望它可以帮助!
#3
19
You create handler in background thread this way
在后台线程中以这种方式创建处理程序
private void createHandler() {
Thread thread = new Thread() {
public void run() {
Looper.prepare();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do Work
handler.removeCallbacks(this);
Looper.myLooper().quit();
}
}, 2000);
Looper.loop();
}
};
thread.start();
}
#4
11
Activity.runOnUiThread()
does not work for me. I worked around this issue by creating a regular thread this way:
Activity.runOnUiThread()对我不起作用。我通过创建一个常规线程来解决这个问题:
public class PullTasksThread extends Thread {
public void run () {
Log.d(Prefs.TAG, "Thread run...");
}
}
and calling it from the GL update this way:
从GL更新中调用:
new PullTasksThread().start();
#5
4
Try running you asyntask from the UI thread. I faced this issue when I wasn't doing the same!
尝试从UI线程运行异步任务。当我不这么做的时候,我面临着这个问题!
#6
2
Try this
试试这个
Handler mHandler = new Handler(Looper.getMainLooper());
mHandler.post(new Runnable() {
@Override
public void run() {
//your code here that talks with the UI level widgets/ try to access the UI
//elements from this block because this piece of snippet will run in the UI/MainThread.
}
});
#1
142
The error is self-explanatory... doInBackground()
runs on a background thread which, since it is not intended to loop, is not connected to a Looper
.
这个错误是不言而喻的…doInBackground()运行在一个后台线程上,由于它不打算循环,所以没有连接到一个循环。
You most likely don't want to directly instantiate a Handler at all... whatever data your doInBackground()
implementation returns will be passed to onPostExecute()
which runs on the UI thread.
您很可能不想直接实例化一个处理程序……无论doInBackground()实现返回什么数据,都将传递给运行在UI线程上的onPostExecute()。
mActivity = ThisActivity.this;
mActivity.runOnUiThread(new Runnable() {
public void run() {
new asyncCreateText().execute();
}
});
ADDED FOLLOWING THE STACKTRACE APPEARING IN QUESTION:
添加以下出现问题的STACKTRACE:
Looks like you're trying to start an AsyncTask
from a GL rendering thread... don't do that cos they won't ever Looper.loop()
either. AsyncTasks are really designed to be run from the UI thread only.
看起来您正在尝试从一个GL呈现线程启动一个AsyncTask……不要这样做,因为它们也不会loop .loop()。AsyncTasks实际上是设计为只从UI线程运行。
The least disruptive fix would probably be to call Activity.runOnUiThread()
with a Runnable
that kicks off your AsyncTask
.
破坏性最小的解决方案可能是调用Activity.runOnUiThread(),并使用一个Runnable来启动异步任务。
#2
28
All the answers above are correct, but I think this is the easiest example possible:
上面所有的答案都是正确的,但我认为这是最简单的例子:
public class ExampleActivity extends Activity {
private Handler handler;
private ProgressBar progress;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progress = (ProgressBar) findViewById(R.id.progressBar1);
handler = new Handler();
}
public void clickAButton(View view) {
// Do something that takes a while
Runnable runnable = new Runnable() {
@Override
public void run() {
handler.post(new Runnable() { // This thread runs in the UI
@Override
public void run() {
progress.setProgress("anything"); // Update the UI
}
});
}
};
new Thread(runnable).start();
}
}
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.
它所做的是,从通过活动中声明的处理程序的post()方法传递的完全不同的线程更新UI线程中的进度条。
Hope it helps!
希望它可以帮助!
#3
19
You create handler in background thread this way
在后台线程中以这种方式创建处理程序
private void createHandler() {
Thread thread = new Thread() {
public void run() {
Looper.prepare();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do Work
handler.removeCallbacks(this);
Looper.myLooper().quit();
}
}, 2000);
Looper.loop();
}
};
thread.start();
}
#4
11
Activity.runOnUiThread()
does not work for me. I worked around this issue by creating a regular thread this way:
Activity.runOnUiThread()对我不起作用。我通过创建一个常规线程来解决这个问题:
public class PullTasksThread extends Thread {
public void run () {
Log.d(Prefs.TAG, "Thread run...");
}
}
and calling it from the GL update this way:
从GL更新中调用:
new PullTasksThread().start();
#5
4
Try running you asyntask from the UI thread. I faced this issue when I wasn't doing the same!
尝试从UI线程运行异步任务。当我不这么做的时候,我面临着这个问题!
#6
2
Try this
试试这个
Handler mHandler = new Handler(Looper.getMainLooper());
mHandler.post(new Runnable() {
@Override
public void run() {
//your code here that talks with the UI level widgets/ try to access the UI
//elements from this block because this piece of snippet will run in the UI/MainThread.
}
});