关于子线程使用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 ...
随机推荐
- poj 1005 I Think I Need a Houseboat
#include <iostream> using namespace std; const double pi = 3.1415926535; int main() { ;; doubl ...
- 全面理解Linux输入输出重定向
全面理解Linux输入输出重定向 本教程通过视频方式讲解shell操作,理解感念,教程通俗易懂,比起看一大堆文档要舒服的多.本次教程主要讲解 Linux Shell 中支持输入输出重定向,用符号&l ...
- html5页面中拨打电话的方式
<a href="tel:18688888888">拨号</a> <a href="sms:18688888888">发短信 ...
- oracle数据库建表
create or replace directory dumpdir as 'E:\oracle\dumpdir';create temporary tablespace ydxt_temp tem ...
- POJ 1039 Pipe(直线和线段相交判断,求交点)
Pipe Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8280 Accepted: 2483 Description ...
- 关于iTunes11.1 不能刷自制固件的解决方案
由于iTunes升级到11.1后, 苹果在程序里限制了不允许刷自制固件, 所以想刷自制固件暂时只能降级iTunes到11.1版本之前, 这里提供iTunes 11.0.5 的下载地址: Windows ...
- svn如何回滚到之前版本
第一种情况:改动没有被提交(commit). 这种情况下,使用svn revert就能取消之前的修改. svn revert用法如下: # svn revert [-R] something 其中so ...
- circle area
circle area Github 链接:传送门 本次作业要求 Create a program that asks for the radius of a circle and prints th ...
- hdoj 5317 RGCDQ
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5317 #include<stdio.h> ; int F[MAXN]; bool flag ...
- Web服务器与Web系统发布
在讨论Web系统发布之前,我们先来辨析两个概念:服务器.Web服务器. 通常,我们说的服务器,是一台提供服务的计算机,是硬件概念.这台主机有其IP地址,有服务端口,我们要访问时,就是通过IP地址唯一地 ...