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 ...
随机推荐
- JavaScript高级程序设计(九):基本概念----函数
一.参数的理解 1.ECMAScript中的参数在内部是用一个数组来表示的.函数接收到的始终是这个数组,而不关心数组中包含多少个参数,即使没有参数也可以. 2.实质上,函数可以通过arguments对 ...
- c#实现FTP上传
/// <summary> /// 上传文件 /// </summary> /// <param name="fileinfo">需要上传的文件 ...
- 程序员面试题精选100题(38)-输出1到最大的N位数[算法]
作者:何海涛 出处:http://zhedahht.blog.163.com/ 题目:输入数字n,按顺序输出从1最大的n位10进制数.比如输入3,则输出1.2.3一直到最大的3位数即999. 分析:这 ...
- Json操作
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- MailOtto 实现完美预加载以及源码解读
背景: 最近项目组需要一个小课题分享,小白刚好从微博里看到一个这样有趣的开源工具MailOtto,是阿里巴巴员工 Drakeet 维护的一个专注懒事件的事件总线,gitHub地址为:https://g ...
- Linux下的I/O复用与epoll详解
前言 I/O多路复用有很多种实现.在linux上,2.4内核前主要是select和poll,自Linux 2.6内核正式引入epoll以来,epoll已经成为了目前实现高性能网络服务器的必备技术.尽管 ...
- asp.net 获取当前项目路径
方法一://获取当前项目的路径System.AppDomain.CurrentDomain.BaseDirectory.ToString(); // 得到的是当前项目的根目录取的值:F://Pro ...
- 前端资源多个产品整站一键打包&包版本管理(四)—— js&css文件文件打包并生成哈希后缀,自动写入路径、解决资源缓存问题。
问题: 当我们版本更新的时候,我们都要清理缓存的js跟css,如何使得在网页中不需要手动清理呢? 答案: 生成带有哈希后缀的js跟css文件 1.文件路径 路径中的conf.js 是用于放置全局打包的 ...
- linux shell命令的常用快捷键
一些shell的常用快捷键. Ctrl + a 切换到命令行开始 Ctrl + e 切换到命令行末尾 Ctrl + l 清除屏幕内容 Ctrl + u 清除剪切光标之前的内容 Ctrl + ...
- Android开发框架
AsyncHttpClient 它把HTTP所有的通信细节全部封装在了内部,我们只需要简单调用几行代码就可以完成通信操作 Universal-Image-Loader 它使得在界面上显示网络图片的操作 ...