Looper.prepare();  // Can't create handler inside thread that has not called Looper.prepare()。
Toast.makeText(context,"登录超时",Toast.LENGTH_SHORT).show();
Looper.loop();

这样子写就不会报错了;

但是他为什么报错???你是不是在子线程显示Toast了;来看一下Toast的源码:

    /**
* Constructs an empty Toast object. If looper is null, Looper.myLooper() is used.
* @hide
*/
public Toast(@NonNull Context context, @Nullable Looper looper) {
mContext = context;
mTN = new TN(context.getPackageName(), looper);
mTN.mY = context.getResources().getDimensionPixelSize(
com.android.internal.R.dimen.toast_y_offset);
mTN.mGravity = context.getResources().getInteger(
com.android.internal.R.integer.config_toastDefaultGravity);
}

TN : 就看最主要这一点吧!

  if (looper == null) {
// Use Looper.myLooper() if looper is not specified.
looper = Looper.myLooper();
if (looper == null) {
throw new RuntimeException(
"Can't toast on a thread that has not called Looper.prepare()");
}
}
mHandler = new Handler(looper, null) {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SHOW: {
IBinder token = (IBinder) msg.obj;
handleShow(token);
break;
}
case HIDE: {
handleHide();
// Don't do this in handleHide() because it is also invoked by
// handleShow()
mNextView = null;
break;
}
case CANCEL: {
handleHide();
// Don't do this in handleHide() because it is also invoked by
// handleShow()
mNextView = null;
try {
getService().cancelToast(mPackageName, TN.this);
} catch (RemoteException e) {
}
break;
}
}
}
};

报错的原因是因为你的looper == null ,myLooper也是null;

Toast的内部会创建Handler,而Handler又依赖于Looper;Looper不存在,自然会报错;

Android主线程会默认创建Looper,而子线程则不会默认创建Looper;所以我们需要手动去创建Looper并开启循环;

Can't create handler inside thread that has not called Looper.prepare()的更多相关文章

  1. 在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()

    在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException:  Can't create handler inside thread that has ...

  2. Android 线程更新UI报错 : Can't create handler inside thread that has not called Looper.prepare()

    MainActivity中有一个按钮,绑定了save方法 public void save(View view) { String title = titleText.getText().toStri ...

  3. 关于子线程使用Toast报错Can't create handler inside thread that has not called Looper.prepare()的解决办法

    形同如下代码,在Thread中调用Toast显示错误信息: new Thread(new Runnable(){ @Override public void run() { try{ weatherD ...

  4. Can’t create handler inside thread that has not called Looper.prepare()

    1)在Android 2.3以前,为防止ANR(Application Not Responding),Google是不赞成将网络连接等一系列耗时操作直接放到应用主线程进行的,推荐将这类操作放在子线程 ...

  5. Android-java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

    章出自:luchg技术交流 http://www.luchg.com 版权所有.本站文章除注明出处外,皆为作者原创文章,可自由引用,但请注明来源,谢谢. Android-java.lang.Runti ...

  6. Android handler 报错处理Can't create handler inside thread that has not called Looper.prepare()

    问题: 写了一个sdk给其他人用,提供一个回调函数,函数使用了handler处理消息 // handler监听网络请求,完成后操作回调函数 final Handler trigerGfHandler ...

  7. Android进阶(八)Can't create handler inside thread that has not called Looper.prepare()

    Error:Can't create handler inside thread that has not called Looper.prepare() 原代码: //利用Handler消息传递机制 ...

  8. Android进阶(十六)子线程调用Toast报Can't create handler inside thread that has not called Looper.prepare() 错误

    原子线程调用Toast报Can't create handler inside thread that has not called Looper.prepare() 错误 今天用子线程调Toast报 ...

  9. 【转】在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()

    在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException:  Can't create handler inside thread that has ...

  10. Android 错误提示: Can't create handler inside thread that has not called Looper.prepare()

    Can't create handler inside thread that has not called Looper.prepare() 将 Handler handler = new Hand ...

随机推荐

  1. 20165308 学习基础和C语言基础调查

    学习基础和C语言基础调查 技能学习 我认为给学生具体的, 能实践的, 能马上看到因果关系的教材和练习, 是激发学生兴趣, 好奇心, 求知欲的好方法. -- 引用自<做中学> 老师博客中注重 ...

  2. 谈谈 数据中心SOA 架构

    为什么要讨论 数据中心SOA 架构呢? 请参考我写的另外一篇文章  <论 微服务 和 Entity Framework 对数据的割裂>    https://www.cnblogs.com ...

  3. 在公司上wifi

    公司的wifi上不了咋办?自己搞! 做法: 把自己的newifi mini带到公司, 记录公司内网ip,登入路由器设置, 1. 把ip改为内网一致, 2.关闭dhcp功能 3.设置wifi 网线连接: ...

  4. centos git server 的搭建

    安装环境  centos7 说明:centos  yum 库里面的git  好像是不区分 客户端和服务器端, 安装 git 以后 就可以创建  仓库,也可以检出 别的 git 仓库的  代码了.所以不 ...

  5. 手动制作openstack windows镜像

    https://docs.openstack.org/image-guide/windows-image.html 这里以 windows 2008为例: 准备工作: 1准备好windows 2008 ...

  6. Hibernate更新删除数据后,再查询数据依然存在的解决办法

    删除数据后,重新查询了数据库,DB中记录已经删除了,但是数据依然能查询到,网上都说是Hibernate的缓冲问题. 我对session进行了clear,flush,并且在事务和查询中都对session ...

  7. Hbase 简介+环境安装配置教程

    HBase介绍 HBase是参考google的bigtable的一个开源产品,建立在hdfs之上的一个提供高可靠性.高性能.列存储.可伸缩.实时读写的数据库系统.是一种介于nosql和RDBMs之间的 ...

  8. Linux mktemp命令

    mktemp命令 Linux mktemp命令用于建立暂存文件.mktemp建立的一个暂存文件,供shell script使用.主要特点就是可以做到每次执行mktemp时产生文件和目录都不重名:这个特 ...

  9. sort a given string

    my solution: function order(words){ var splitToArr = words.split(' '), len = splitToArr.length, newA ...

  10. ubuntu-docker入门到放弃(一)docker的安装

    基于ubuntu14.04系统 安装参考官网:https://docs.docker.com 1.首先检查是否安装过docker,如果安装过,卸载: sudo apt-get remove docke ...