开发错误记录1:解决:Only the original thread that created a view hierarchy can touch its views.
今天在项目中要使用圆角头像,导入开源 CircleImageView ,然后setImageBitmap()时
运行时就会发现,它会报一个致命性的异常::
· ERROR/AndroidRuntime(421): FATAL EXCEPTION: Thread-8
· ERROR/AndroidRuntime(421): android.view.ViewRoot$CalledFromWrongThreadException:
· Only the original thread that created a view hierarchy can touch its views.
原因在于,Android系统中的视图组件并不是线程安全的,如果要更新视图,必须在主线程中更新,不可以在子线程中执行更新的操作。检查代码发现是在ui线程中调用的呀!后换成系统控件ImageView,一切正常,这是咋个回事?原代码:
public void setAvatar(final String imgurl) {
if (imgurl == null || user == null) return;
try {
URL url = new URL(imgurl);
user.setImageBitmap(BitmapFactory.decodeStream(url.openStream());
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
后发现处理网络请求时,要用单独线程,把代码更成如下:
private Bitmap bm = null;
public void setAvatar(final String imgurl) {
if (imgurl == null || user == null) return;
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(imgurl);
bm=BitmapFactory.decodeStream(url.openStream());
} catch (Exception e) {
e.printStackTrace();
}
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
user.setImageBitmap(bm);
}
});
}
}).start();
}
因为android开发,要碰到很多定制过的系统,有时会遇到有些品牌机可以正常运行,有些不能!
耗时,网络请求操作一定要单独开线程操作,这样就能减少不同机型兼容的问题!
开发错误记录1:解决:Only the original thread that created a view hierarchy can touch its views.的更多相关文章
- 解决Only the original thread that created a view hierarchy can touch its views
这种异常出现在子线程中处理UI操作产生的异常,将UI操作放在主线程中就OK了
- 浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.
在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...
- Only the original thread that created a view hierarchy can touch its views解决办法
这周操作系统作业布置了一个作业,内容是做个小软件,来模拟消费者生产者问题,作业实现起来不来,因为之前写过这个算法,所以关键步骤就是在消费和生产的时候更新缓存区的UI控件就行,之后问题就来了,出现了标题 ...
- 错误:Only the original thread that created a view hierarchy can touch its views——Handler的使用
在跟随教程学习到显示web页面的html源码时报错:Only the original thread that created a view hierarchy can touch its views ...
- Only the original thread that created a view hierarchy can touch its views
在调试软件的时候出现如下的错误: 01-05 20:53:36.492: E/ZZShip(2043): android.view.ViewRootImpl$CalledFromWrongThread ...
- Android: Only the original thread that created a view hierarchy can touch its views 异常
最近自己再写一个小项目练手,创建一个线程从网络获取数据然后显示在 recyclerView 上.写好后发现页面能够显示,但是有时候会把请求的数据显示过来,有时候不会.点开 android monito ...
- 子线程调用invalidate()产生“Only the original thread that created a view hierarchy can touch its views.”原因分析
目录 1.异常出处 2.从View.invalidate()方法开始分析 3.ViewRootImpl如何与View进行关联:从Activity的setContentView开始分析 3.1 最顶层的 ...
- andriod 错误:Only the original thread that created a view hierarchy can touch its views——Handler的使用
package com.example.yanlei.myapplication; import android.media.MediaMetadataRetriever; import androi ...
- "Only the original thread that created a view hierarchy can touch its views.” 解决方法
这个主要总是,开启的线程和 UI 线程(主线程)不是同一个线程.可以Runnable方式避免,如下例所示就可以解决这个问题了. public static void updateText(Activi ...
随机推荐
- KVM 介绍(5):libvirt 介绍 [ Libvrit for KVM/QEMU ]
学习 KVM 的系列文章: (1)介绍和安装 (2)CPU 和 内存虚拟化 (3)I/O QEMU 全虚拟化和准虚拟化(Para-virtulizaiton) (4)I/O PCI/PCIe设备直接分 ...
- django csrf 处理简介
CSRF 是什么 CSRF 即跨站请求伪造,在用户不知情的情况下向有漏洞的网站发送请求.例如有正常网站A,恶意网站B, 用户若对A B 两个网站都有访问,B 可能伪造请求到 A,比如提交表单.至于具体 ...
- POJ2488A Knight's Journey[DFS]
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41936 Accepted: 14 ...
- POJ1014Dividing[多重背包可行性]
Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 68769 Accepted: 17955 Descri ...
- AC日记——加密的病历单 openjudge 1.7 12
12:加密的病历单 总时间限制: 1000ms 内存限制: 65536kB 描述 小英是药学专业大三的学生,暑假期间获得了去医院药房实习的机会. 在药房实习期间,小英扎实的专业基础获得了医生的一致 ...
- 终于可以在centos下使用QQ啦!
电脑装了centos 6.4操作系统,一直无法使用QQ,在centos中文论坛看到一篇介绍安装qq的文章,依样画葫芦,终于成功了1.下载QQ2012软件安装包,我给大家准备好了下载地址 [root@b ...
- u-boot移植初步尝试-tiny4412
获取u-boot源代码 在u-boot官方网站下载uboot源码.ftp://ftp.denx.de/pub/u-boot/ 因为是第一次移植uboot,所以这里选的版本是 u-boot-2013.0 ...
- Lambda表达式详解(转载)
原文链接:http://www.cnblogs.com/knowledgesea/p/3163725.html lambda简介 lambda运算符:所有的lambda表达式都是用新的lambda运算 ...
- curl命令
定位后端接口是否ok,经常使用到curl -b/cookie <name=string/file> cookie字符串或文件读取位置 curl http://localhost --co ...
- 后台运行程序screen or nohup
后台运行 方法1 & 方法2:screen screen –S lnmp à起个名字 进去后运行程序 Ctrl+ad à退出lnmp屏幕 Scree –ls à查看 Screen –r x ...