更新是由于在新的线程来打开UI只有一个错误。子线程更新主线程UI需要使用Handler.

还有比如今天出现以下错误。码,如以下:

send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "主线程中UI", Toast.LENGTH_SHORT).show();
}
});
}
});
Toast.makeText(getApplicationContext(), "主线程UI", Toast.LENGTH_SHORT).show();  

都是这句话惹的祸。

把上面这行代码也通过处理放进handler之后。错误就没有了。

Handler是这么定义的:

主要接受子线程发送的数据, 并用此数据配合主线程更新UI.

Handler的主要作用:主要用于异步消息的处理

Handler的执行过程:

当(子线程)发消息后,。首先,进入一个(主线程)消息队列,函数来发送消息立即返回,在主线程Handler一个在新闻删除消息队列中的一个,然后处理该消息。这样就实现了跨线程UI更新(其实在主线程中仍然完成)。

这种机构通常被用来处理通过相对耗时较长的操作,如访问网络是耗时的操作,读大的文本文件,比较费时的操作处理。

[Android]Can't create handler inside thread that has not called Looper.prepare()的更多相关文章

  1. 使用ClipboardManager碰到Can't create handler inside thread that has not called Looper.prepare()

    直接放上我的代码.希望能给碰到相同问题的朋友提供帮助 Runnable runnable = new Runnable() { public void run() { ClipboardManager ...

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

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

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

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

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

  6. 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消息传递机制 ...

  7. Android 错误提示: Can't create handler inside thread that has not called Looper.prepare()

    Can't create handler inside thread that has not called Looper.prepare() 将 Handler handler = new Hand ...

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

  9. Can’t create handler inside thread that has not called Looper.prepare()

    1)在Android 2.3以前,为防止ANR(Application Not Responding),Google是不赞成将网络连接等一系列耗时操作直接放到应用主线程进行的,推荐将这类操作放在子线程 ...

随机推荐

  1. [linux]chown和chmod命令

    chown chown命令是将指定文件的拥有者改为指定的用户或组 例如: chown mail:mail test.log,把test文件指定拥有者和组都为mail chown -R mail:mai ...

  2. jQuery 自学笔记—5 遍历

    什么是遍历? jQuery 遍历,意为“移动”,用于根据其相对于其他元素的关系来“查找”(或选取)HTML 元素.以某项选择开始,并沿着这个选择移动,直到抵达您期望的元素为止. 下图展示了一个家族树. ...

  3. ActivityGroup简单介绍

    ActivityGroup简单介绍 1.ActivityGroup的核心就是继承了该类,可以通过getLocalActivityManager()得到一个LocalActivityManager 如, ...

  4. Objective-C之成魔之路【16-使用文件】

    郝萌主倾心贡献,尊重作者的劳动成果.请勿转载. 假设文章对您有所帮助,欢迎给作者捐赠.支持郝萌主,捐赠数额任意.重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 语言的设计 ...

  5. WP开发使用BingMaps地图服务

    原文:WP开发使用BingMaps地图服务 WP8使用BingMaps地图在 SOAP服务如何计算路径 首先需要用到3个服务 1.GeoCode服务-转换地址到地理的经纬度(WebServices地址 ...

  6. Java的内存泄漏和垃圾回收机制

    JAVA会产生内存泄露吗?首先,答案是肯定的. Java尽管有垃圾回收器,但依旧存在泄漏. Java内存泄漏跟C/C++内存泄漏的概念不一样:C/C++的内存泄漏是指Malloc了一些资源.最后没有f ...

  7. CF552E 字符串 表达式求值

    http://codeforces.com/contest/552/problem/E E. Vanya and Brackets time limit per test 1 second memor ...

  8. c#为了实现自己的线程池功能(一)

    线程池的技术背景 在面向对象编程中,创建和销毁对象是非常费时间的,由于创建一个对象要获取内存资源或者其他很多其他资源,所以提高服务程序效率的一个手段就是尽可能降低创建和销毁对象的次数.特别是一些非常耗 ...

  9. POJ 1146:ID Codes

    ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6281 Accepted: 3769 Description ...

  10. gitLab添加ssh key

    电脑新装了一台虚拟机,想要和gitLab建立一个安全的ssh连接,步骤如下 1.本机生成ssh key 系统环境:Linux 使用root用户登录,执行命令:ssh-keygen -t rsa -C ...