在线程中调用包含创建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. c 判断水仙花数,质数(素数)

    #include<stdio.h> #include<stdbool.h> //水仙花数--各位立方和等于本身 void sXh() { int x,y,z; printf(& ...

  2. UIScreen的 bound、frame、scale属性

    CGRect bound = [[UIScreen mainScreen] bounds];  // 返回的是带有状态栏的Rect   CGRect frame = [[UIScreen mainSc ...

  3. Thread.Sleep(0)的意义 操作系统中CPU的竞争策略

    在线程的学习中遇到的 不太明白就搜了一下  有一篇觉得写得很好的分享一下 转载:http://www.360doc.com/content/12/1220/07/1054746_255212714.s ...

  4. LeetCode 二叉树后序遍历(binary-tree-postorder-traversal)

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  5. Java之JDOM生成XML和解析

    一.生成XML文件 1.JDOM是对Java原始的类进行了封装.让解析XML文件变得很方便 2.创建一个XML文件的根节点: Element root = new Element("HD&q ...

  6. A.归并排序

    归并排序 (求逆序数) 归并排序:递归+合并+排序 时间复杂度:O(n logn)    空间复杂度:O(n) 用途:1.排序  2.求逆序对数 Description In this problem ...

  7. octopress command memo

    1 rake new_post rake new_post[title]           # Begin a new post in source/_posts 2 rake preview ht ...

  8. Android显示GIF动画完整示例(一)

    MainActivity如下: package cc.testgif; import com.ant.liao.GifView; import com.ant.liao.GifView.GifImag ...

  9. eclipse config 3 构造pydev

    什么是不是说生命是短暂的.我用python 准备工作 sudo apt-get install python3-dev 例如以下操作 依次点击菜单 Help->Install New Softw ...

  10. TCP/IP之TCP交互数据流、成块数据流

    建立在TCP协议上的网络协议有telnet,ssh,ftp,http等等.这些协议根据数据吞吐量来分成两大类: (1)交互数据类型,例如telnet,ssh,这种类型的协议在大多数情况下只是做小流量的 ...