开发错误记录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 ...
随机推荐
- 证明你是你——快速开启Windows Azure多重身份验证
中国版Windows Azure的多重身份验证(Multi-Factor Authentication)功能已经开放.这个功能说白了就是要“证明你是你”.目前可以支持以下几种验证方式: 手机,短信验证 ...
- ssh连接失败,排错经验
一.场景描述 ssh连接服务器,发现连接失败,但是对应服务器的ip能够ping通. 场景: [root@yl-web ~]# ssh root@10.1.101.35 ssh_exchange_ide ...
- BZOJ2763[JLOI2011]飞行路线 [分层图最短路]
2763: [JLOI2011]飞行路线 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2523 Solved: 946[Submit][Statu ...
- MSDN论坛被垃圾信息刷爆了!!!
https://social.msdn.microsoft.com/Forums/zh-CN/caab1275-103e-470e-8888-ca39d1c48364/linehx2888?forum ...
- luogu[2093]零件分组
题目描述 某工厂生产一批棍状零件,每个零件都有一定的长度(Li)和重量(Wi).现在为了加工需要,要将它们分成若干组,使每一组的零件都能排成一个长度和重量都不下降(若i<j,则Li<=Lj ...
- html实现弹框,并伴随遮罩层,且弹框居中
本文介绍的内容主要实现的功能有,出现弹框,并且伴随遮罩层,且弹框一直居中. html和js代码: <div id="hidebg"></div> <d ...
- HTML 学习笔记(表格)
HTML 表格 HTML中的表格使用标签<table>来实现,每个表格均有若干行由<tr>标签来定义,每个<tr>表示一行.美航被分为若干个单元格用<td&g ...
- History 对象
History 对象 History 对象包含用户(在浏览器窗口中)访问过的 URL. History 对象是 window 对象的一部分,可通过 window.history 属性对其进行访问. 注 ...
- http应用优化和加速说明-负载均衡
负载均衡技术 现代企业信息化应用越来越多的采用B/S应用架构来承载企业的关键业务,因此,确保这些任务的可靠运行就变得日益重要.随着越来越多的企业实施数据集中,应用的扩展性.安全性和可靠性也 ...
- PAT 1006. 换个格式输出整数 (15)
让我们用字母B来表示"百".字母S表示"十",用"12...n"来表示个位数字n(<10),换个格式来输出任一个不超过3位的正整数.例 ...