开发错误记录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 ...
随机推荐
- 第四章 分治策略 4.2 矩阵乘法的Strassen算法
package chap04_Divide_And_Conquer; import static org.junit.Assert.*; import java.util.Arrays; import ...
- [转]TCP协议中的三次握手和四次挥手(图解)
本文转自:http://blog.csdn.net/whuslei/article/details/6667471 建立TCP需要三次握手才能建立,而断开连接则需要四次握手.整个过程如下图所示: 先来 ...
- Android工程师入门(二)——不忙不累怎么睡。。
安卓开发迫在眉睫,这周入个门吧! Android工程师入门(二) 四.在界面中显示图片 ImageView 是显示图片的一个控件. --属性 src——内容图片: background——背景图片/背 ...
- 以后上午就只能这样了么-jQuery
hi 昨天睡得不错 为什么早上还是看不进论文,宁愿做这个,也不愿认真看论文.感觉上还是下午看论文感觉要好的多.不过最近有三十多篇要看哇...管球... 1.jQuery -----jQuery常用插件 ...
- .Net程序员之Python基础教程学习----字符串的使用 [Second Day]
在The FirstDay 里面学习了列表的元组的使用,今天开始学习字符串的使用.字符串的使用主要要掌握,字符串的格式化(C语言中我们应该都知道,Python和C语言差别不大),字符串的基本 ...
- 关于TP3.2微信开发那点事(基础篇)
许久没有为博客更新内容,今天我将过去一周做的微信服务号的相关心得体会在此分享,具体如何申请成为服务号的相关流程文档都有,可根据要求完成: 开发第一步:开发前配置: AppID-->微信号的&qu ...
- [tem]高精度2
从1开始 ; ; struct big{ int size,d[L]; big():size(a){memset(d,,sizeof(int)*L);} }; void jia(big &a, ...
- 第9章 用内核对象进行线程同步(1)_事件对象(Event)
9.1 等待函数 (1)WaitForSingleObject(hObject,dwMilliseonds); ①dwMilliseconds为INFINITE时表示无限等待 ②dwMilliseco ...
- memcache的安装和使用
Memcache Memcached是一个高性能的分布式缓存系统.memcached自身不会实现分布式,分布式是由程序来实现的. Memcached一旦安装之后,自身进行管理!预申请一个很大的内存空间 ...
- Android app 简单的电话拨号器
实现步骤: 1.画UI 可以用拖拽和文本编辑. 2.根据UI写业务逻辑 在MainActivity中的onCreate中编写 //get editText content et_number = ( ...