开发错误记录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 ...
随机推荐
- VMworld 2015 感受:VMware “Ready For Any”
今年有机会参加在旧金山举行的 VMworld 2015.今天是正式开始的第一天.争取每天写一篇文章分享所见所听所感.第一天的主要活动,包括上午的 General Session,由 VMware 的几 ...
- HashMap的工作原理深入再深入
前言 首先再次强调hashcode (==)和equals的真正含义(我记得以前有人会说,equals是判断对象内容,hashcode是判断是否相等之类): equals:是否同一个对象实例.注意,是 ...
- 深入理解FTP协议
文件传输协议FTP(File Transfer Protocol)是因特网中使用最广泛的文件传输协议.FTP使用交互式的访问,允许客户指定文件的类型和格式(如指明是否使用ASCII码),并允许文件具有 ...
- Codeforces Round #371 (Div. 2) C. Sonya and Queries[Map|二进制]
C. Sonya and Queries time limit per test 1 second memory limit per test 256 megabytes input standard ...
- git的两本推荐书
1. pro git, 可以网页直接看 http://iissnan.com/progit/?spm=5176.100239.blogcont5843.18.nUJDcK 2. Git权威指南 < ...
- Eclipse自动打开实现类原型的工具
http://eclipse-tools.sourceforge.net/implementors/ I always use this implementors plugin to find all ...
- HTML 学习笔记 CSS样式(文本)
CSS文本属性可以定义文本的外观 通过文本属性 您可以改变文本的颜色 字符间距 文本对齐装饰文本 对文本进行缩进等等. 缩进文本 把web页面上的段落的第一行缩进,这是最常用的文本格式化效果. css ...
- TP框架实现分页
TP框架自带分页的实现方法,所以使用这个分页方案,不用再重新造轮子 1,先看效果图 2,源码 /** * TODO 基础分页的相同代码封装,使前台的代码更少 * @param $m 模型,引用传递 * ...
- 十个节省时间的MySQL命令
十个节省时间的MySQL命令 2011-02-23 16:07 黄永兵 译 IT168 字号:T | T 编者在工作中积累起来了一些MySQL命令行客户端技巧,这些技巧或多或少会帮助您节省大量的时间. ...
- iTextSharp带中文转换出来的PDF文档显示乱码
刚才有写一个小练习<Html代码保存为Pdf文件>http://www.cnblogs.com/insus/p/4323224.html.马上有网友说,当截取块有中文时,保存的pdf文件将 ...