关于子线程使用Toast报错Can't create handler inside thread that has not called Looper.prepare()的解决办法
形同如下代码,在Thread中调用Toast显示错误信息:
new Thread(new Runnable(){
@Override
public void run() {
try{
weatherData = getWeatherData(strUrl);
parseJson(weatherData);
}catch(Exception e){
Toast.makeText(WindowApplication.getAppContext(), e.toString(), Toast.LENGTH_SHORT).show();
Log.i("wytings",e.toString());
}
}
}).start();
一运行,就会报错Can't create handler inside thread that has not called Looper.prepare(),因为Toast的初始化函数中,自己开了个线程new Handler();所以使得当前的Toast要是不在主线程就会报错。
解决办法如下,在Toast上下添加Looper.prepare();和Looper.loop();
Looper.prepare();
Toast.makeText(WindowApplication.getAppContext(), e.toString(), Toast.LENGTH_SHORT).show();
Looper.loop();
关于子线程使用Toast报错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 ...
- 工具类ToastUtil 避免在子线程中使用抛异常 "Can't create handler inside thread that has not called Looper.prepare()"
package com.example.kbr.utils; import android.view.Gravity; import android.widget.Toast; import io.r ...
- 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报 ...
- Android handler 报错处理Can't create handler inside thread that has not called Looper.prepare()
问题: 写了一个sdk给其他人用,提供一个回调函数,函数使用了handler处理消息 // handler监听网络请求,完成后操作回调函数 final Handler trigerGfHandler ...
- OkHttp下载文件中途断网报Can't create handler inside thread that has not called Looper.prepare()异常的解决办法
最近做项目时出现个问题. 在一个基类中,创建一个Handler对象用于主线程向子线程发送数据,代码如下: this.mThirdHandler = new Handler(){ @Override p ...
- adapter中报错:Can't create handler inside thread that has not called Looper.prepare()
http://stackoverflow.com/questions/9357513/cant-create-handler-inside-thread-that-has-not-called-loo ...
随机推荐
- 第三百二十一天 how can I 坚持
上班第一天,感觉时间过得好慢. 心里好烦,做什么都没心情,感觉没有勇气了,虽然早上说了那么多,但不敢去面对了. 咋整? <猪老三><野子>. 好想去看<美人鱼> 不 ...
- Message Forwarding
[Preprocess] 在使用forwarding机制前,会先经历2个步骤,只有当这2个步骤均失败的情况下,才会激活forwarding. 1.+(BOOL)resolveInstanceMetho ...
- centos安装lxml和pyspider
yum -y install --nogpgcheck python34u-devel.x86_64 yum -y install libcurl-devel yum -y install libxs ...
- B - Kefa and Company
B - Kefa and Company Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I6 ...
- 陕西中际现代:基于自适应算法的PLC滴灌控制系统
基于自适应算法的PLC滴灌控制系统 陕西中际现代包装科技有限公司滴灌部 1.介绍 水资源正在成为一种珍贵的资源.城镇的市民使用成千上万立方的水来浇灌花园和绿地.他们依赖于使用固定灌溉计划的控制器.而这 ...
- IAP内购
IAPHelper.h // // IAPHelper.h // airplay // // Created by apple on 13-10-23. // Copyright (c) 2013年 ...
- 用SSH连接SSH连接nitrous.io
CSDN怎么传不上图片... http://user.qzone.qq.com/1756942789/blog/1388662053 1:先下载Bitvise SSH 2:打开 keypair man ...
- Flex data
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...
- Winform XiaoCai.WinformUI 框架界面设计
开源用户界面和布局的套件XiaoCai.WinformUI(美化用户界面利器) http://www.cnblogs.com/aganqin/p/3400453.html 源码下载:https://g ...
- jeecg团队招新人(5人)
jeecg团队招新人(5人) http://www.jeecg.org/forum.php? mod=viewthread&tid=2046&page=1&extra=#pid ...