在线程中调用包含创建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. VMware: linux起步提示 memory for crashkernel(0*0 to 0*0)not within permissible

    (virtualbox/VMware)linux起步提示memoryforcrashkernel(0*0 to 0*0)notwithinpermissible  http://www.myexcep ...

  2. 如何隐藏DLL中,导出函数的名称?

    一.引言 很多时候,我们写了一个Dll,不希望别人通过DLL查看工具,看到我们的导出函数名称.可以通过以下步骤实现: 1. 在def函数中做如下定义: LIBRARY EXPORTS HideFunc ...

  3. BZOJ 2196: [Usaco2011 Mar]Brownie Slicing( 二分答案 )

    二分答案就可以了.... ----------------------------------------------------------------------- #include<cst ...

  4. github每次push都需要密码以及用户名的解决办法

    git remote set-url origin git@github.com:你的账户/项目名称.git就可以直接git push origin master了.

  5. S2S:分享出的营销机遇

    (速途网专栏 作者:高学争)经常在网上购物的你,有没有遇到过这样的问题:你知道自己想买什么,但是在淘宝上一搜,出来了数以万计的同类型,他们有着同样的价位(甚至可能是同样的图片),但是由不同的商家提供, ...

  6. Android绘图监听接口OnPreDrawListener详解

    public static interface ViewTreeObserver.OnPreDrawListener 我们先看下API中的定义: 类概述: 为即将绘制视图树时执行的回调函数定义的接口. ...

  7. 驱动:中断【2】中断处理程序、中断上下文中处理延时及一些函数的调用规则(调IIC中断驱动有感)

    中断处理程序.中断上下文中处理延时及一些函数的调用规则(调IIC中断驱动有感)http://blog.csdn.net/samantha_sun/article/details/6790492 1,中 ...

  8. linux 工具: Top

    linux TOP命令各参数详解[转载] http://www.cnblogs.com/sbaicl/articles/2752068.html

  9. jquery.zclip—复制剪贴板(兼容各大浏览器)

    开始前说两句 WEB开发中,要让用户复制页面中的一段代码.URL地址等信息,为了避免用户拖动鼠标再进行右键复制操作而可能出现的差错,我们可以直接在页面中放置一个复制按钮,只需要轻轻一点这个复制按钮,内 ...

  10. docker 学习笔记20:docker守护进程的配置与启动

    安装好docker后,需要启动docker守护进程.有多种启动方式. 一.服务的方式 因为docker守护进程被安装成服务.所以,可以通过服务的方式启停docker守护进程,包括查看状态. sudo ...