情景
HomeActivity试图包含了Fragment,fragment中在okhttp的回调函数中 修改了view的控件,报错Only the original thread that created a view hierarchy can touch its views
解决办法:
方法1、在fragment中创建主线程的实例
调用
MainActivity.this.runOnUiThread(new Runnable() { public void run() { ... } });
方法2、
重写handler
private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { if (msg.what == 1) { ... } } };
调用
new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } handler.sendEmptyMessage(1); } }).start();
或者直接handler.sendMessage(1)