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

在一个基类中,创建一个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. PPAPI与Browser间使用AsyncIPC通信

    採用AsyncIpc这个项目(https://github.com/hicdre/AsyncIpc).来完毕PPAPI Plugin进程与Browser进程的通信. foruok原创.如需转载请关注f ...

  2. (31)java web的hibernate使用-一级缓存,二级缓存

    参考:https://blog.csdn.net/miachen520/article/details/52195832 hibernate自带一级缓存 和 二级缓存 一,一级缓存: 基于Sessio ...

  3. (linux)likely和unlikely函数

      在Linux内核中likely和unlikely函数有两种(只能两者选一)实现方式,它们的实现原理稍有不同,但作用是相同的,下面将结合linux-2.6.38.8版本的内核代码来进行讲解. 1.对 ...

  4. jquery 获取radio被选中的值

    <html> <head> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"& ...

  5. linux初级学习笔记五:bash特性详解!(视频序号:03_2,3)

    本节学习的命令:history,alias,ualias,\CMD 本节学习的技能:   bash的特性 光标跳转 查看命令历史 命令历史的使用技巧 给命令起别名 命令替换 文件名通配符 shell: ...

  6. 2014山东省“浪潮杯”第五届ACM省赛总结

    一次比赛做一次总结,弱菜又来总结了…… 我这种大四的又死皮赖来混省赛了,貌似就我和山大威海的某哥们(不详其大名)了吧.颁奖前和他聊天,得知他去百度了,真是不错,ORZ之. 比赛流水账: 题目目前不知道 ...

  7. 利用百度地图API制作房产酒店地图

    摘要: 想亲手制作一张酷讯.去哪儿.安居客.链接地产那样的房产.酒店地图麼?那赶快来学习吧.(以酷讯为例,如下图) 更多成功案例请点击:http://dev.baidu.com/wiki/map/in ...

  8. python中的异常处理try/except/finally/raise

    异常发生在程序执行的过程中,如果python无法正常处理程序就会发生异常,导致整个程序终止执行,python中使用try/except语句可以捕获异常. try/except 异常的种类有很多,在不确 ...

  9. Caffe-Windows下遇到过的问题、技巧、解决方案

    转换数据,求均值: 转换数据 步骤大概是:建立一个train文件夹,里面放一个train.txt;建立一个test文件夹,里面放一个test.txt,然后分别运行以下两条bat命令: SET GLOG ...

  10. Android Studio下载安装

    官方下载地址:https://developer.android.google.cn/studio#downloads 因为安卓自带的模拟器会比较慢一些,这里勾选去掉,我们使用夜神模拟器. 这里根据自 ...