报错 Can't create handler inside thread Thread[OkHttp http://greencode.lmding.com/...,5,main] that has not called Looper.prepare()
时间: 2024-05-29 10:10:01 浏览: 296
这个错误通常是因为在非 UI 线程中尝试更新 UI,或者在没有调用 Looper.prepare() 的线程中尝试创建 handler。要解决这个问题,可以尝试以下几种方法:
1. 在 UI 线程中更新 UI,或者使用 runOnUiThread() 方法来在 UI 线程中执行操作。
2. 在非 UI 线程中使用 Looper.prepare() 和 Looper.loop() 方法来创建一个消息循环,然后再创建 handler。
3. 使用 Android 提供的异步任务 AsyncTask 来执行后台操作并更新 UI。
4. 使用 Android 提供的 HandlerThread 来创建一个带有消息循环的线程,并在其中创建 handler。
总之,要避免在非 UI 线程中尝试更新 UI 或者创建 handler,以避免出现这个错误。
阅读全文
相关推荐


















