Android安卓 Can't create handler inside thread Thread[Thread-2,5,main] that has not called Looper.prepare()

  • 错误复现

在使用okhttp的时候新建了一个thread,在回调函数中创建了handler准备更新ui

  • 错误原因

不能在子线程更新ui,需要回到主线程

  • 解决办法

使用new Handler(Looper.getMainLooper());

  • 代码样例
Handler handler = new Handler(Looper.getMainLooper());
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            //更新ui操作
                        }
                    });
posted @ 2021-11-11 17:04  起个名怎么这么难  阅读(1432)  评论(0编辑  收藏  举报