形同如下代码,在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()的解决办法的更多相关文章

  1. 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 ...

  2. 在子线程中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 ...

  3. 【转】在子线程中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 ...

  4. 转 在子线程中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 ...

  5. 工具类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 ...

  6. 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报 ...

  7. Android handler 报错处理Can't create handler inside thread that has not called Looper.prepare()

    问题: 写了一个sdk给其他人用,提供一个回调函数,函数使用了handler处理消息 // handler监听网络请求,完成后操作回调函数 final Handler trigerGfHandler ...

  8. OkHttp下载文件中途断网报Can't create handler inside thread that has not called Looper.prepare()异常的解决办法

    最近做项目时出现个问题. 在一个基类中,创建一个Handler对象用于主线程向子线程发送数据,代码如下: this.mThirdHandler = new Handler(){ @Override p ...

  9. 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 ...

随机推荐

  1. Struts – Wildcards example

    Struts wildcards can helps to reduce the repetition in your struts-config.xml file, as long as your ...

  2. Linux查找文件夹名

    @(编程) find / -type d -name filename type的类型 -type c File is of type c: b block (buffered) special c ...

  3. java实现图片与base64字符串之间的转换

    package cn.com; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOEx ...

  4. MSDN上面测试Window的方法(很好用)

    如何:将 Windows 服务作为控制台应用程序运行 向你运行 OnStart 和 OnStop 方法的服务添加一个方法:     internal void TestStartupAndStop(s ...

  5. Codeforces 467C. George and Job (dp)

    题目链接:http://codeforces.com/contest/467/problem/C 求k个不重叠长m的连续子序列的最大和. dp[i][j]表示第i个数的位置个序列的最大和. 前缀和一下 ...

  6. Windows 10 Edge浏览器、照片查看程序关闭“平滑滚动”

    升级到10后,这两个常用软件的“平滑滚动”功能,个人感觉体验有点不好,特别是图片这个自带程序,看了几十张图后就有点头晕了,所以把它关闭为好: 控制面板\系统和安全\系统\高级系统设置\高级\性能\设置 ...

  7. OC:NSmuber、NSString 的互转

    NSmuber 转化为 IOS 中的 NSString 假设现有一NSNumber的变量A,要转换成NSString类型的B 方法如下: NSNumberFormatter* numberFormat ...

  8. Ubuntu创建launcher

    创建Launcher 以创建pycharm的launcher为例 创建文件pycharm.desktop 编辑该文件加入如下行: [Desktop Entry] Name=Pycharm #显示名称 ...

  9. 关于刘冬大侠Spring.NET系列学习笔记3的一点勘正

    诚如他第22楼“只因渴求等待”提出的疑问一样,他的下面那一段代码是存在一点点问题的, XElement root = XElement.Load(fileName); var objects = fr ...

  10. 转载:DIV+CSS有可能遇到的问题

    [总结]DIV+CSS有可能遇到的问题 一.超链接访问过后hover样式就不出现的问题? 被点击访问过的超链接样式不在具有hover和active了,解决方法是改变CSS属性的排列顺序: L-V-H- ...