这周操作系统作业布置了一个作业,内容是做个小软件,来模拟消费者生产者问题,作业实现起来不来,因为之前写过这个算法,所以关键步骤就是在消费和生产的时候更新缓存区的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的源码也没整得太明白,可能因为我接触安卓不太久的原因,火候还不够,但是问题摆在这里还是得解决...我还是贴个大佬很详细的讲解吧

【Android源码解析】View.post()到底干了啥

里面有很详细的代码讲解,一行一行跟进的

另外,还贴几个更新在子线程中更新UI的方法

安卓在子线程更新UI的几种方法

Only the original thread that created a view hierarchy can touch its views解决办法的更多相关文章

  1. "Only the original thread that created a view hierarchy can touch its views.” 解决方法

    这个主要总是,开启的线程和 UI 线程(主线程)不是同一个线程.可以Runnable方式避免,如下例所示就可以解决这个问题了. public static void updateText(Activi ...

  2. Android: Only the original thread that created a view hierarchy can touch its views 异常

    最近自己再写一个小项目练手,创建一个线程从网络获取数据然后显示在 recyclerView 上.写好后发现页面能够显示,但是有时候会把请求的数据显示过来,有时候不会.点开 android monito ...

  3. 开发错误记录1:解决:Only the original thread that created a view hierarchy can touch its views.

    今天在项目中要使用圆角头像,导入开源 CircleImageView ,然后setImageBitmap()时 运行时就会发现,它会报一个致命性的异常:: · ERROR/AndroidRuntime ...

  4. 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 ...

  5. 浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.

    在分析Android消息机制之前,我们先来看一段代码: public class MainActivity extends Activity implements View.OnClickListen ...

  6. 错误: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 ...

  7. 子线程调用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 最顶层的 ...

  8. 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 ...

  9. 解决Only the original thread that created a view hierarchy can touch its views

    这种异常出现在子线程中处理UI操作产生的异常,将UI操作放在主线程中就OK了

随机推荐

  1. 2018.12.31 bzoj3992: [SDOI2015]序列统计(生成函数+ntt+快速幂)

    传送门 生成函数简单题. 题意:给出一个集合A={a1,a2,...as}A=\{a_1,a_2,...a_s\}A={a1​,a2​,...as​},所有数都在[0,m−1][0,m-1][0,m− ...

  2. (18)What a planet needs to sustain life

    https://www.ted.com/talks/dave_brain_what_a_planet_needs_to_sustain_life/transcript 00:12I'm really ...

  3. HDU 2147 kiki's game (奇偶博弈)

    题意:给定一个 n * m 的格子,从右上角(1, m) 开始每个玩家只能从向下,向左,或者向左下走,谁不能走,谁输. 析:自己做出来,看了网上的几个博客,好像都没说为什么是只有全奇的情况才会输,个人 ...

  4. DocumentType类型

    并不常用 nodeType 10 nodeName doctype的名称 NodeValue 值为null parentNode Document 不支持子节点 DocumentType不能动态创建, ...

  5. GPT分区在IBM服务器上安装linux不能引导的解决方法

    提示: Your boot partition is on a disk using the GPT partitioning Scheme but this machines cannot boot ...

  6. 20155326 《Java程序设计》第8周学习总结

    20155326 <Java程序设计>第8周学习总结 教材学习内容总结 NIO (1)NIO使用频道来衔接数据节点,在处理数据时,NIO可以让你设定缓冲区容量,在缓冲区中对感兴趣的数据区块 ...

  7. 【MySQL】死锁问题分析

    1.MySQL常用存储引擎的锁机制: MyISAM和MEMORY采用表级锁(table-level locking)   BDB采用页面锁(page-level locking)或表级锁,默认为页面锁 ...

  8. java中逗号分隔的字符串和List相互转换

    1.将逗号分隔的字符串转换为List String str = "a,b,c"; List<String> result = Arrays.asList(str.spl ...

  9. hdu 5089 使做对k-1题最大概率的选题方案

    http://acm.hdu.edu.cn/showproblem.php?pid=5089 给出N道难度递增的题目,难度用可能做出的百分比表示,选出K道题目使得做出K-1道题目的概率最大. 选k题的 ...

  10. Scala_继承

    继承 Scala与Java在继承方面的区别 Scala中的继承与Java有着显著的不同: 重写一个非抽象方法必须使用override修饰符 只有主构造器可以调用超类的主构造器 在子类中重写超类的抽象方 ...