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

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

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

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

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

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

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

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

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

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. javascript if(xx)

    js判断某个值知否存在或者为空,可以直接用if(xx)过滤. <!DOCTYPE html> <html> <head> <meta charset=&quo ...

  2. xunit输出output到控制台

    1.https://xunit.github.io/docs/capturing-output 里面似乎提到2个方法,第二个方法还需要在配置文件中添加appSetting 这里采用第一种方法, 1.添 ...

  3. PHP Framework MVC Benchmark 基准测试

    身边有朋友在用yaf框架,讨论的也声音也比较多,今天没事看鸟哥的博客,看到一篇现在PHP主流的几个框架性能对比,比较有意思,给大家分享一下! Yaf是用PHP扩展的形式写的一个PHP框架,也就是以C语 ...

  4. iOS成员变量、实例变量、属性变量三者的联系与区别

    一.类Class中的属性property 在ios第一版中: 我们为输出口同时声明了属性和底层实例变量,那时,属性是oc语言的一个新的机制,并且要求你必须声明与之对应的实例变量,例如: 注意:(这个是 ...

  5. MyBatis相关资源

    MyBatis很多项目中有用到,但会用并不表示你真正理解它,更不代表你能很清楚的教会别人.如果想在会用它的基础上更深入的通过学习它而提升自己技术能力,可利用下面资源. 1.官网文档,基本概念讲的很清楚 ...

  6. 微信小程序-B站:wxml和wxss文件

    WXML WXML(WeiXin Markup Language)是微信的一套标签语言,结合基础组件.事件系统,可以构建出页面的结构. (小安娜:好像很厉害的样子,那基础组件.事件系统是什么?感觉更厉 ...

  7. egret 和cocos2d-x-js哪个目前更稳定更好用? ?

    问题: 貌似cocos名气大一些?因为神经猫的大火才知道egret,玩了一下他们的demo,貌似性能一般,不过对flash开发者特别亲切.有人对比过这两个引擎吗?分析下 百牛信息技术bainiu.lt ...

  8. 监听屏幕旋转事件window. onorientationchange

    // 判断屏幕是否旋转 function orientationChange() { switch(window.orientation) { case 0: alert("肖像模式 0,s ...

  9. 收藏产品判断、html 在 UIwebView里面显示

     收藏产品功能 要求:用户点击收藏,如果已经收藏,用户点击就取消收藏 写法一: 点击事件{ if (!isSelect) { [sender setImage:[UIImage imageNamed: ...

  10. USACO 奶牛排队

    题目:给出一个只含有1,2,3的数字序列,问最少交换多少次才能将之变为递增数列. 解: 注意到只有1,2,3,我们只要将1,3交换到自己的应在位置上那么排序就已经完成了. 需要交换的有几种,记$a(x ...