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
这句话的意思是,只有创建这个view的原始线程才能接触到它,意思就是我UI控件的更新不在原始线程里面,我源码这样的
//Producter.java
switch (producter.charAt(producter.length()-1)){
case '0':
view0.setImageResource(R.drawable.goods);
Log.d("生产者:我做了",producter);
break;
其实到这里我大概知道了我为什么错了,可是又不想重写逻辑,换到主线程来改UI,于是查了一下资料,在安卓中,UI更新不是线程安全的,所以只能在主线程中更新UI,那么,到底有没有问题在子线程中更新UI呢,答案肯定是可以的,我找到了一个办法,那就是通过View类中的post函数来更新,首先我把做法的源码贴上来
switch (producter.charAt(producter.length()-1)){
case '0':
view0.post(new Runnable() {
@Override
public void run(){
view0.setImageResource(R.drawable.goods);
Log.d("生产者:我做了",producter);
}
});
break;
可以看到,这里启动了一个新的线程,但是,问题来了,为什么这样就可以了?UI更新不还是在非主线程中吗?
说实话我想了挺久没想太明白,看了一下view.post的源码也没整得太明白,可能因为我接触安卓不太久的原因,火候还不够,但是问题摆在这里还是得解决...我还是贴个大佬很详细的讲解吧
里面有很详细的代码讲解,一行一行跟进的
另外,还贴几个更新在子线程中更新UI的方法
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 线程(主线程)不是同一个线程.可以Runnable方式避免,如下例所示就可以解决这个问题了. public static void updateText(Activi ...
- Android: Only the original thread that created a view hierarchy can touch its views 异常
最近自己再写一个小项目练手,创建一个线程从网络获取数据然后显示在 recyclerView 上.写好后发现页面能够显示,但是有时候会把请求的数据显示过来,有时候不会.点开 android monito ...
- 开发错误记录1:解决:Only the original thread that created a view hierarchy can touch its views.
今天在项目中要使用圆角头像,导入开源 CircleImageView ,然后setImageBitmap()时 运行时就会发现,它会报一个致命性的异常:: · ERROR/AndroidRuntime ...
- 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.
在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...
- 错误: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 ...
- 子线程调用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操作产生的异常,将UI操作放在主线程中就OK了
随机推荐
- 2018.10.25 atcoder Leftmost Ball(计数dp+组合数学)
传送门 dp妙题啊. 我认为DZYODZYODZYO已经说的很好了. 强制规定球的排序方式. 然后就变成了一个求拓扑序数量的问题. 代码: #include<bits/stdc++.h> ...
- s5-11 距离矢量路由选择协议
距离矢量路由选择(Distance Vector:DV) 每个路由器维护一张表,表中列出了当前已知的到每个目标 的最佳距离,以及为了到达那个目标,应该从哪个接口转发. 距离矢量路由选择(Distanc ...
- Python处理微信利器——itchat
接触itchat是一个偶然,上知乎刷出一个有意思的文章.于是乎运行源码,调错加上查阅博客,发现itchat大有可为. 知乎链接:https://zhuanlan.zhihu.com/p/2578293 ...
- SecureCRTv7.3 和 navicat110_mysql
激活步骤: 一.首次使用: 1.保持SecureCRT未打开. 2.打开注册机keygen.exe文件(Windows vista ,7,8需要以管理员身份运行),点击[Patch]按钮,会让你选择文 ...
- mysql同时使用order by和limit查询时的一个严重隐患 -- 丢失数据
转自: https://blog.csdn.net/tsxw24/article/details/44994835 我经常使用order by和limit来做数据分页显示并排序,一直也没发现过什么问题 ...
- PAT甲级 1120. Friend Numbers (20)
1120. Friend Numbers (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Two in ...
- 差值的再议-Hermite差值
1. 插值法 插值法又称“内插法”,是利用函数f (x)在某区间中已知的若干点的函数值,作出适当的特定函数,在区间的其他点上用这特定函数的值作为函数f (x)的近似值,这种方法称为插值法. 如果这特定 ...
- 如何防止ElasticSearch集群出现脑裂现象
什么是“脑裂”现象? 由于某些节点的失效,部分节点的网络连接会断开,并形成一个与原集群一样名字的集群,这种情况称为集群脑裂(split-brain)现象.这个问题非常危险,因为两个新形成的集群会同时索 ...
- Stringbuffer与substring
1. Stringbuffer 有append()方法 Stringbuffer 其实是动态字符串数组 append()是往动态字符串数组添加,跟“xxxx”+“yyyy”相当那个‘+’号 跟Stri ...
- Swift3 重写一个带占位符的textView
class PlaceStrTextView: UIView,UITextViewDelegate{ var palceStr = "即将输入的信息" //站位文字 var inp ...