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

时间:2011-02-15 21:36:21

标签: android runtimeexception

我有一个活动,并且我有一个班级。

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) {

    }
}

我明白了:

  

ERROR / 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

7 个答案:

答案 0 :(得分:145)

错误是不言自明的...... doInBackground()在后​​台线程上运行,因为它不打算循环,所以没有连接到Looper

您很可能根本不想直接实例化Handler ...无论您将doInBackground()实现返回的数据传递给在UI线程上运行的onPostExecute()

   mActivity = ThisActivity.this; 

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

在问题中出现的问题后添加:

看起来你正在尝试从GL渲染线程中启动AsyncTask ...不要这样做,他们也不会Looper.loop()。 AsyncTasks实际上只能从UI线程运行。

破坏性最小的解决方案可能是通过Activity.runOnUiThread()拨打Runnable来启动AsyncTask

答案 1 :(得分:31)

以上所有答案都是正确的,但我认为这是最简单的例子:

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();
    }
}

这样做是从通过活动中声明的处理程序的post()方法传递的完全不同的线程更新UI线程中的进度条。

希望它有所帮助!

答案 2 :(得分:20)

您可以通过这种方式在后台线程中创建处理程序

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();
    }

答案 3 :(得分:11)

Activity.runOnUiThread()对我不起作用。我通过这种方式创建常规线程来解决这个问题:

public class PullTasksThread extends Thread {
   public void run () {
      Log.d(Prefs.TAG, "Thread run...");
   }
}

以这种方式从GL更新中调用它:

new PullTasksThread().start();

答案 4 :(得分:4)

尝试从UI线程运行asynctask。当我没有做同样的事情时,我遇到了这个问题!

答案 5 :(得分:2)

试试这个

    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.                                     
                            }
                        });

答案 6 :(得分:0)

对于所有 Kotlin 爱好者,这里是给你的代码

Handler(Looper.getMainLooper()).post {
      // any UI update here
}