安卓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更改操作
          }
      }
    };

就可以完成事务处理啦~

你可能感兴趣的:(安卓Only the original thread that created a view hierarchy can touch its views)