最近做项目时出现个问题。

在一个基类中,创建一个Handler对象用于主线程向子线程发送数据,代码如下:

this.mThirdHandler = new Handler(){
@Override
public void handleMessage(android.os.Message msg) {
super.handleMessage(msg);
Bundle bundle = msg.getData();
isStop = bundle.getBoolean(mContext.getText(R.string.str_message_stop).toString());//isStop为基类中的一个私有成员
};
};

但不知道为啥一直报错:Can't create handler inside thread that has not called Looper.prepare()。

搜索后发现,原因是此Handler没有Looper。到哪儿去找Looper呢?自己建?

在代码前加入Looper.prepare();,心想这回可以了吧?

没想到依然报错,错误显示,一个主进程只能有一个Looper,要死了。郁闷中...

突然我想到主进程中肯定有Looper,Context.getMainLooper(),再看Handler的实例化时是可以指定Looper的,太爽了,最后代码如下

this.mThirdHandler = new Handler(mContext.getMainLooper()){
@Override
public void handleMessage(android.os.Message msg) {
super.handleMessage(msg);
Bundle bundle = msg.getData();
isStop = bundle.getBoolean(mContext.getText(R.string.str_message_stop).toString());
};
};

mContext为主界面context,实例化基类时引入的一个参数。

仅供学习参考,如有不足,欢迎指导。

OkHttp下载文件中途断网报Can't create handler inside thread that has not called Looper.prepare()异常的解决办法的更多相关文章

  1. 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报 ...

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

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

  3. 在子线程中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 ...

  4. 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 ...

  5. 关于子线程使用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 ...

  6. 【转】在子线程中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 ...

  7. 转 在子线程中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 ...

  8. 在okhttp的callback回调中加Toast出现Cant create handler inside hread that has not called Looper.prepare()...

    2019独角兽企业重金招聘Python工程师标准>>> 分析:callback中回调的response方法中还是在子线程中运行的,所以要调取Toast必须回到主线程中更新ui 解决方 ...

  9. windows下编译caffe报错:error MSB4062: 未能从程序集 E:\NugetPackages\OpenCV.2.4.10\......的解决办法

    参考博客:http://blog.csdn.net/u013277656/article/details/75040459 在windows上编译caffe时,用vs打开后会自动加载还原NugetPa ...

随机推荐

  1. (linux)SD卡初始化-mmc_sd_init_card函数(续)

      转自:http://www.cnblogs.com/fengeryi/p/3472728.html   mmc_sd_init_card剩下的关于UHS-I的分支结构. uhs-I的初始化流程图如 ...

  2. ap和路由器有什么区别 ap和路由器的区别介绍【图文】

    现在能够摆脱网线限制能够自由方便上网的WiFi和无线网络也来越流行,很多酒店.饭店.宾馆.办公楼等地方都会提供无线网络.而能够提供无线网络的设备有很多,现在我们介绍的是无线ap和无线路由器.那么,ap ...

  3. CodeForces-884D:Boxes And Balls(合并石子)

    Ivan has n different boxes. The first of them contains some balls of n different colors. Ivan wants ...

  4. web自动化测试的自身特点

    1.web页面是出现的元素可能具有不确定性 2.不同操作系统上不同web浏览器之间的兼容性 3.web应用的高并发性和容错性 4.移动设备上web客户端兼容性,旋转下和各种触摸特性

  5. 【扬中集训DAY1T1】 微信群

    [题目链接] 点击打开链接 [算法] 对问题稍加分析后,发现其实要求的就是 : C(N,K) + C(N,K+1) + C(N,K+2) + ... + C(N,N) 因为N最大10^9,K最大10^ ...

  6. unittest参数化parameterized

    参考文章: https://www.cnblogs.com/royfans/p/7226360.html https://blog.csdn.net/zha6476003/article/detail ...

  7. 文件的创建,读取,写入,修改,删除---python入门

    转自:http://blog.163.com/jackylau_v/blog/static/175754040201181505158356/ 一.用Python创建一个新文件,内容是从0到9的整数, ...

  8. 006--linux基础rpm和yum的使用和源码安装

    一.rpm相关命令介绍 1. 查看CD里面有的文件 2. 用rpm来安装一个名为vsftpd的rpm包 3. rpm -qi 软件包名 (查看软件包的详细信息) 4. rpm -ql 软件包名 (查看 ...

  9. PaaS服务之路漫谈(二)

    此文已由作者尧飘海授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 天下大势,分久必合,合久必分,社会历史的发展方向总有着惊人的相似.把这种规律应用到软件应用架构的发展方向上, ...

  10. 如何用GO实现一个tail -f功能以及相应的思维发散

    此文已由作者杨望暑授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 背景 在服务端查看log会经常使用到tail -f命令实时跟踪文件变化. 那么问题来了, 如果自己写一个同样 ...