Can't create handler inside thread which has not called Looper.prepare()


I have an Activity, and in that I have a class.

text=new Dynamictext(...);

in my DynamicText java I have this code:

public void setText(String text) {
    new asyncCreateText().execute();

//private Handler handler = new Handler();

private class asyncCreateText extends AsyncTask<Void, Void, Void> 
    protected Void doInBackground(Void... unused) {
        return null;

    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(
ERROR/AndroidRuntime(5370):     at com.l.start.OpenGLRenderer.initfonts(
ERROR/AndroidRuntime(5370):     at com.l.start.OpenGLRenderer.init(
ERROR/AndroidRuntime(5370):     at com.l.start.OpenGLRenderer.onSurfaceChanged(
ERROR/AndroidRuntime(5370):     at android.opengl.GLSurfaceView$GLThread.guardedRun(
ERROR/AndroidRuntime(5370):     at android.opengl.GLSurfaceView$

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>(
ERROR/AndroidRuntime(5370):     at android.os.AsyncTask$InternalHandler.<init>(
ERROR/AndroidRuntime(5370):     at android.os.AsyncTask$InternalHandler.<init>(
ERROR/AndroidRuntime(5370):     at android.os.AsyncTask.<clinit>(
ERROR/AndroidRuntime(5370):     ... 6 more
9/30/2015 12:43:45 PM

Accepted Answer

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.

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.

   mActivity = ThisActivity.this; 

    mActivity.runOnUiThread(new Runnable() {
     public void run() {
     new asyncCreateText().execute();


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.

The least disruptive fix would probably be to call Activity.runOnUiThread() with a Runnable that kicks off your AsyncTask.

2/16/2016 10:42:43 PM

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;

    public void onCreate(Bundle savedInstanceState) {
        progress = (ProgressBar) findViewById(;
        handler = new Handler();

    public void clickAButton(View view) {
        // Do something that takes a while
        Runnable runnable = new Runnable() {
            public void run() {
       Runnable() { // This thread runs in the UI
                    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.

Hope it helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow