Can't create handler inside thread that has not called Looper.prepare()
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()的更多相关文章
- 在子线程中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 ...
- 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 ...
- 关于子线程使用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 ...
- Can’t create handler inside thread that has not called Looper.prepare()
1)在Android 2.3以前,为防止ANR(Application Not Responding),Google是不赞成将网络连接等一系列耗时操作直接放到应用主线程进行的,推荐将这类操作放在子线程 ...
- 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 ...
- Android handler 报错处理Can't create handler inside thread that has not called Looper.prepare()
问题: 写了一个sdk给其他人用,提供一个回调函数,函数使用了handler处理消息 // handler监听网络请求,完成后操作回调函数 final Handler trigerGfHandler ...
- 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消息传递机制 ...
- 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报 ...
- 【转】在子线程中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 ...
- 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 ...
随机推荐
- Java ThreadPoolTaskExecutor使用
1. 配置 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http:/ ...
- Typescript学习总结之接口
接口 用来建立某种代码约定,使得其他开发者在调用某个方法或者创建新的类时必须遵守接口所定义的代码约定 1. 接口声明属性 interface IStudent { name: string; age: ...
- [转]TA-Lib 安装
转自:https://mrjbq7.github.io/ta-lib/install.html Installation You can install from PyPI: $ pip instal ...
- RedHat6.5创建本地yum源
拷贝Linux操作系统的rhel-server-6.5-x86_64-dvd.iso镜像到装好的Linux中: #执行挂载命令 mount –o loop /media/rhel-server-6.5 ...
- 多层级sql查询
SELECT ( SELECT ) FROM t_app_user WHERE parent_user_id = t.user_id /*多层级,t.user_id */ ) AS directCou ...
- ML平台_微博深度学习平台架构和实践
( 转载至: http://www.36dsj.com/archives/98977) 随着人工神经网络算法的成熟.GPU计算能力的提升,深度学习在众多领域都取得了重大突破.本文介绍了微博引入深度学 ...
- linux的系统组成和计算机组成原理,linux常用操作
Linux入门 linux简介 学习目的:linux服务器操作系统稳定长期运行,python,pycharm装于linux上 linux系统组成 应用软件:调用系统软件接口 linux操作系统分两 ...
- 前端之js-echarts组件介绍
Echarts.init: 全局 echarts 对象,在 script 标签引入 echarts.js 文件后获得,或者在 AMD 环境中通过 require('echarts') 获得. < ...
- IIS7 UNC File caching issue
You have to either choose dir-monitoring and file-change-notification with its drawback of using SMB ...
- PrintWriter中的write与println方法居然就是这些区别
为什么循环中分别用write方法和println方法效果一样呢? import java.io.*; public class WriteLog { private BufferedReader bf ...