在线程中调用包含创建handler方法的时候,会报错,提示:

“need call Looper.prepare()” -- 在创建之前,调用Looper.prepare()方法来创建一个looper

但是这个包含创建handler的方法,可能在主线程中调用,也可能在子线程中调用。

在主线程中调用的时候,你给它加上looper.prepare()方法,它可能会报错,提示:

“Only one Looper may be created per thread” -- 每个线程之中,只能有一个Looper

解决的办法:

        	if (Looper.myLooper() == null) {
Looper.prepare();
}

其中,Looper.myLooper()方法的返回值是当前线程的Looper对象,返回的是Null的时候,则需要通过Looper.prepare()方法创建Looper

可以对照Looper类的源码查看一下,即可。

android handler looper thread的更多相关文章

  1. android Handler、Thread和Runnable

    android里面的创建的Handler对象并不是新建一个新的线程,而是在主线程执行,主线程的消息队列中循环. java中实现一个线程有两种方法,一种是继承Thread类,一种是实现Runnable接 ...

  2. android handler looper

    http://www.cnblogs.com/plokmju/p/android_Handler.html

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

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

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

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

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

  7. 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消息传递机制 ...

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

  9. Android之消息机制Handler,Looper,Message解析

    PS:由于感冒原因,本篇写的有点没有主干,大家凑合看吧.. 学习内容: 1.MessageQueue,Looper,MessageQueue的作用. 2.子线程向主线程中发送消息 3.主线程向子线程中 ...

随机推荐

  1. SAE部署Java应用

    链接地址:http://blog.csdn.net/shuixin536/article/details/9031335 SAE为开发者提供了非常宽松的开发环境,你甚至不用做任何特别定制就能将各种Ja ...

  2. mysql性能调优与架构设计(一)商业需求与系统架构对性能的影响

    这里我们就拿一个看上去很简单的功能来分析一下. 需求:一个论坛帖子总量的统计附加要求:实时更新 在很多人看来,这个功能非常容易实现,不就是执行一条SELECT COUNT(*)的Query 就可以得到 ...

  3. Android 学习开发笔记《Android认识》

    1.          Android:2007年11月5日第一版,2009年5月豪华版,2010年HTC手机 2.          Android框架主要:应用程序.应用程序框架.函数库.运行时. ...

  4. Java疯狂讲义

  5. js 几个特殊情况

    alert(033-15);//12,前缀0用在直接量中,表示八进制 alert('033'-15);//18,前缀0用在字符串中,在(隐式)转换将忽略 alert(parseInt('033')-1 ...

  6. 一維條碼編碼規則(1D Barcode)

    1.Code 39 條碼:又分 標準型Code 39 條碼(Standard Code 39):資料內容包含有0~9數字,A~Z英文字母,”+”,”-“,”*”,”/”,”%”,”$”,”.”以及sp ...

  7. codility上的练习 (1)

    codility上面添加了教程.目前只有lesson 1,讲复杂度的……里面有几个题, 目前感觉题库的题简单. tasks: Frog-Jmp: 一只青蛙,要从X跳到Y或者大于等于Y的地方,每次跳的距 ...

  8. ASP.NET 内置对象涉略

    一.ASP.NET中内置的常用对象的介绍 本文列举了ASP.NET 的八个内置对象,其中前五个是比较常用的. 1.Response Response 对象用于从服务器向用户发送输出的结果. Write ...

  9. Js基础操作

    var a="zhangsan"; document.write(a+":I love JavaScrip"); a="lisi"; doc ...

  10. c++,类的组合

    1. 在A类中以B类的对象作为成员变量,称为类的组合(composition). 可以先看看这篇文章理解组合的用处: C++中组合的使用 http://blog.csdn.net/jia_xiaoxi ...