这周操作系统作业布置了一个作业,内容是做个小软件,来模拟消费者生产者问题,作业实现起来不来,因为之前写过这个算法,所以关键步骤就是在消费和生产的时候更新缓存区的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. (15)3 kinds of bias that shape your worldview

    https://www.ted.com/talks/j_marshall_shepherd_3_kinds_bias_that_shape_your_worldview/transcript 00:1 ...

  2. matlab柱面图

    f=@(x,y)log(y);  % ln(x)函数,平行于x轴ezsurf(f,[-pi*2,pi*2,0,20])

  3. C++ cout

    cout.flush() cout.put() 输出一个字符 char* p = "hello"; cout.write(p,sttrlen(q)-3) 输出字符串,能够选定长度. ...

  4. Jpa 报错 :HTTP Status 400 - Required String parameter 'xx' is not present

    一.问题描述 使用Springboot JPA 做分页查询,报错Required String parameter 'xx' is not present,后端未接受到请求 二.解决方案: 使用的请求 ...

  5. highChart图表

    Highcharts 是一个用纯JavaScript编写的一个图表库, 能够很简单便捷的在web网站或是web应用程序添加有交互性的图表,并且免费提供给个人学习.个人网站和非商业用途使用.HighCh ...

  6. 为什么要重写hashCode()方法和equals()方法及如何重写

    我想写的问题有三个: 1.首先我们为什么需要重写hashCode()方法和equals()方法 2.在什么情况下需要重写hashCode()方法和equals()方法 3.如何重写这两个方法 **** ...

  7. day35(servlet 3.0)

    servlet3.0的问题 Servlet3.0与Servlet2.5提供了三个新特性: * 注解开发   :方便 * 文件上传   :有些API不是特别全. * 异步请求   :多线程的实现 注解开 ...

  8. Hdu1695 GCD 2017-06-27 22:19 30人阅读 评论(0) 收藏

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  9. 两数据库Dblink数据抽取blob

    在目标数据库建一张临时表Create global temporary table test3 on commit preserve rows as select * from TEST1 ;在目前数 ...

  10. Chubby是什么?

    先简单的理解,以后补充: 为了解决hadoop分布式系统的一致性问题 ,有很多人提出很多protocol,其中就有有名的Paxos算法(Latex作者提出,算法需要学习), 但是Chubby并不是一个 ...