Android handler 报错处理Can't create handler inside thread that has not called Looper.prepare()
问题:
写了一个sdk给其他人用,提供一个回调函数,函数使用了handler处理消息
// handler监听网络请求,完成后操作回调函数
final Handler trigerGfHandler = new Handler() {
public void handleMessage(Message msg) {
listener.onGeofenceTrigger(gfMatchIds);
}
};
在使用这个sdk提供的函数时,报错:
01-02 15:46:10.498: E/AndroidRuntime(16352): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
使用方式是在service中使用。在activity中使用正常。
问题解决:
在调用handler的方法前执行Looper.prepare()。Looper用于封装了android线程中的消息循环,默认情况下一个线程是不存在消息循环(message loop)的,需要调用Looper.prepare()来给线程创建一个消息循环,调用Looper.loop()来使消息循环起作用。
因为activity的主线程是自己创建消息队列的,所以在Activity中新建Handler时,不需要先调用Looper.prepare()
代码:
Looper.prepare();
// handler监听网络请求,完成后操作回调函数
final Handler trigerGfHandler = new Handler() {
public void handleMessage(Message msg) {
listener.onGeofenceTrigger(gfMatchIds);
}
};
Looper.loop();
参考:http://www.myexception.cn/mobile/410671.html
========================
新问题:
当此函数在activity中调用时,也会报错,因为activity已经自动创建了消息队列。所以重复创建会出错。
查看官网关于Looper的例子:
class LooperThread extends Thread {
public Handler mHandler; public void run() {
Looper.prepare(); mHandler = new Handler() {
public void handleMessage(Message msg) {
// process incoming messages here
}
}; Looper.loop();
}
}
将handler写在一个线程中。使此消息队列与主线程中消息队列分离。
最终写法为:
class LooperThread extends Thread { public void run() {
Looper.prepare();
trigerGfHandler = new Handler() {
public void handleMessage(Message msg) {
// process incoming messages here
listener.onGeofenceTrigger(gfMatchIds);
}
};
Looper.loop();
}
} LooperThread looper = new LooperThread();
looper.start();
Android handler 报错处理Can't create handler inside thread that has not called Looper.prepare()的更多相关文章
- 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 ...
- 在子线程中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 ...
- 【转】在子线程中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 ...
- 转 在子线程中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 ...
- 关于子线程使用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 ...
- 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报 ...
- OkHttp下载文件中途断网报Can't create handler inside thread that has not called Looper.prepare()异常的解决办法
最近做项目时出现个问题. 在一个基类中,创建一个Handler对象用于主线程向子线程发送数据,代码如下: this.mThirdHandler = new Handler(){ @Override p ...
- 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消息传递机制 ...
- 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 ...
随机推荐
- SimpleDateFormat 的性能和线程安全性
系统正常运行一段时间后,QA报给我一个异常: java.lang.OutOfMemoryError: GC overhead limit exceeded at java.text.DecimalFo ...
- selenium2.0处理case实例(二)
本文通过具体代码处理过程, 来展示selenium中一些比较不常用的类的用法 1.javascriptExcutor,通过将driver强转成JavascriptExecutor类型, 调用execu ...
- There is no Action mapped for namespace [/] and action name [user] associated with context path
从c++转到java,初学struts,竟然碰到一个因写错单词而造成的错误,structs --> struts
- 05_例子讲解:rlCollisionDemo.exe
碰撞检测的例子: "E:\Program Files (x86)\rl-0.6.2\bin\rlCollisionDemo.exe" "E:\Program Files ...
- Web前端新人笔记之jquery入门
本章将为大家介绍以下几点内容: 1.jquery的主要特点: 2.建立jquery的编码环境: 3.简单jquery脚本示例: 4.选择jquery而不是纯javaScript的理由: 5.常用的jq ...
- opensuse13.1 双屏幕扩展
最近搞一项j2eeweb工程,想要使用Linux平台编写程序.对比Ubuntu 14.04.Redhat.openSUSE,选择了最后这个. 安装的时候将/boot只分了100M,后来空间不足软件都安 ...
- WPF中添加Ribbon遇到的问题
很奇怪的说,当我新建WPF工程,添加RibbonControlsLibary.dll后会运行时会报错,System.Windows.Markup.XamlParseException.引发的异常信息为 ...
- windows 下 scrapy的安装
安装参考博客:http://davenzhang.com/scrapy_install.htm 我是先安装了scrapy,发现import scrapy 的时候报错.之后一次安装了下面关联软件的.ex ...
- Dev-C++之开启装逼效果
Dev-C++是个不错的C++IDE——在10年前,它是很不错,在现在,它是个以界面丑陋和调试像吃粑粑这两点著称,如下图.
- NGUI系列教程五(角色信息跟随)
在一些网络游戏中,我们常常可以看到角色的上方显示着角色的名称,等级,血量等信息.它们可以跟随角色移动,并且可以显示和隐藏.今天我们就来学习一下这些功能的实现方法.1. 新建unity工 程,导入NGU ...