安卓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进行更新,那怎么办呢,于是就用到了线程通信。
1、首先设置一个标志符号

private static final int COMPLETED = 0;

2、设置发送事务信息方式

Message message = new Message();
message.what = COMPLETED;
handler.sendMessage(message);

3、设置通信处理机制,就可以把UI更改放在里面

    private Handler handler = new Handler(){
      @Override
      public void handleMessage(Message msg) {
          if (msg.what == COMPLETED) {
              imageView.setImageResource(R.drawable.finger); //UI更改操作
          }
      }
    };

就可以完成事务处理啦~

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容