您的位置:首页 > 运维架构

Can't toast on a thread that has not called Looper.prepare()

2017-09-29 11:02 686 查看

前言

最近一段时间在忙着开发一款自己的APP,将自己常用的功能需求都加入进入,同时在GitHub上跟着大牛们学习新的技术,提升自己的技能,在开发的过程中不断的发现问题和解决问题。

在开发过程遇到了这样一个问题:
Can't toast on a thread that has not called Looper.prepare()
,如果在一个线程中没有调用Looper.prepare(),就不能在该线程中创建Toast。这个问题是因为在子线程中弹出Toast导致的。

Android是不能直接在子线程中弹出Toast的,可是如果我们非要这么做,那该怎么办呢?下面就为大家讲解如何在子线程中弹出Toast,以及一些其他类似的子线程中操作的错误。

本博客同步发布于 XueLong的博客



在子线程中调用Toast

在子线程中弹出Toast,会报错:java.lang.RuntimeException: Can’t toast on a thread that has not called Looper.prepare()。

解决方式:先调用
Looper.prepare();
再调用
Toast.makeText().show();
最后再调用
Looper.loop();


public class ToastUtils {
static Toast toast = null;
public static void show(Context context, String text) {
try {
if(toast!=null){
toast.setText(text);
}else{
toast= Toast.makeText(context, text, Toast.LENGTH_SHORT);
}
toast.show();
} catch (Exception e) {
//解决在子线程中调用Toast的异常情况处理
Looper.prepare();
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
Looper.loop();
}
}
}


在子线程中更新UI

在子线程中更新UI,会报错:android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

解决方式:在子线程中更新UI,一般使用Handler或者runOnUiThread()或者AsyncTask。

在子线程中创建Handler

在子线程中创建Handler,会报错:java.lang.RuntimeException: Can’t create handler inside thread that has not called Looper.prepare()。

解决方式:

new Thread() {
  public void run() {
    Looper.prepare();
    new Handler().post(runnable);//在子线程中直接去new 一个handler
    Looper.loop();    //这种情况下,Runnable对象是运行在子线程中的,可以进行联网操作,但是不能更新UI
  }
}.start();


写在最后

以上就是在子线程中更新UI、弹出Toast、创建Handler时会遇到的问题,及解决方式。

如果你在参考过程中遇到问题,可以在我的联系方式中给我提问。

后面会继续介绍,Android的相关知识,欢迎继续关注我博客的更新。

参考资源

在子线程中new Handler报错

Android – Looper.prepare()和Looper.loop() —深入版

Toast和Looper、Handler消息循环机制

转载请注明:XueLong的博客 » Can’t toast on a thread that has not called Looper.prepare()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐