Error:Can't create handler inside thread that has not called Looper.prepare()

原代码:

//利用Handler消息传递机制

final Handler myHandler = new Handler(MainActivity.this.getMainLooper()){

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

if (msg.what == 0x123){

//置于此处有问题 (初步估计是线程间问题)

Toast toast = Toast.makeText(MainActivity.this, "祝您旅途愉快!", Toast.LENGTH_LONG);

toast.show();

}

}

};

//发送空消息

myHandler.sendEmptyMessage(0x123);

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

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

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

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

//利用Handler消息传递机制

final Handler myHandler = new Handler(MainActivity.this.getMainLooper()){

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

if (msg.what == 0x123){

//置于此处有问题 (初步估计是线程间问题)

Toast toast = Toast.makeText(MainActivity.this, "祝您旅途愉快!", Toast.LENGTH_LONG);

toast.show();

}

}

};

//发送空消息

myHandler.sendEmptyMessage(0x123);

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

  1. Android Exception 13(Can't create handler inside thread that has not called Looper.prepare())

    10-12 17:02:55.500: E/AndroidRuntime(28343): FATAL EXCEPTION: Timer-2 10-12 17:02:55.500: E/AndroidR ...

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

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

  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. Android handler 报错处理Can't create handler inside thread that has not called Looper.prepare()

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

  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. Can’t create handler inside thread that has not called Looper.prepare()

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

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

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

随机推荐

  1. python实现购物车

    一. 功能: 1. 用户充值余额 判断余额输入格式是否正确,正确则转换成float型. 2. 显示商品列表 根据已有商品显示所有商品的序号.商品名称.和价格供用户选择 3. 用户选择商品 判断用户输入 ...

  2. Eleven

    A. Eleven time limit per test : 1 second memory limit per test:  256 megabytes input:  standard inpu ...

  3. 到底什么是集群&分布式

    对于楼主这样工作一年的菜鸟,偶尔会看到一些文章标题带有"分布式""集群"关键字,然后就懵逼了.最近对这些概念进行了一定的了解,整理了一下思路,在这里分享给各位猿 ...

  4. Android简易实战教程--第三十八话《自定义通知NotifiCation》

    上一篇小案例,完成了一个普通的通知,点击通知启动了一个活动.但是那里的通知没有加入些"靓点",这一篇就给它加入自定义的布局,完成自定义的通知. 应用:比如QQ音乐为例,当点击音乐播 ...

  5. Scala: 简介和安装

    http://blog.csdn.net/pipisorry/article/details/52902117 Note: lz只是稍微学学,能看懂就行,不深入.适合scala小白. Scala简介 ...

  6. 24 服务AIDL

    AIDL远程访问服务的方法 创建一个接口类写上方法 然后修改后缀java为aidl 在服务中创建一个类继承Stub类 在远程访问服务的进程把AIDL文件复制(包名不能改变) XXXX.Stub.asI ...

  7. @property的参数

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51353580 本文出自:[openXu的博客] 参数类别 参数 说明 原子性 atomic ...

  8. pipeline(管道)设计模式

  9. maven的UnsupportedClassVersionError

    问题描述 我安装了maven3.3.3,配置好了M2_HOME和path环境变量之后,执行mvn -v报错:java.lang.UnsupportedClassVersionError: -. : U ...

  10. 看见的力量 – (I) 解题的思维

    本文转自台湾李智桦老师的博客,原文地址 这篇文章:已经梗了我三个多星期了.这期间飞了二次大陆做演讲.往返几个大城市做教授敏捷开发运用在精实创业的课程.教材内容都是简体的,它们始终没有机会在国内用上,心 ...