"Only the original thread that created a view hierarchy can touch its views."解决
2014-10-29 16:24
453 查看
Android系统中的视图组件并不是线程安全的,如果要更新视图,必须在主线程中更新,不可以在子线程中执行更新的操作。所以可以依靠消息机制来进行更新。
先声明一个handler来处理消息
然后在视图中触发的事件的方法中发送消息到handler来触发更新视图
这样子就可以实现在其他线程中操作主视图。
先声明一个handler来处理消息
private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { setContentView(child); } };
然后在视图中触发的事件的方法中发送消息到handler来触发更新视图
b2 = (Button) findViewById(R.id.return_button); b2.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { setWebView(); } });
private void setWebView() { Message msg = new Message(); msg.what = CHANGE_WEB; handler.sendMessage(msg); }
这样子就可以实现在其他线程中操作主视图。
相关文章推荐
- "Only the original thread that created a view hierarchy can touch its views.” 解决方法
- Android初学习 - 解决 "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
- 开发中错误记录:解决:Only the original thread that created a view hierarchy can touch its views.
- 浅析Android中的消息机制-解决: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
- 浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.
- 浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.
- Work 工作子线程更新UI控件--解决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
- 浅析Android中的消息机制-解决: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
- 已解决:Only the original thread that created a view hierarchy can touch its views.
- android开发中出现Only the original thread that created a view hierarchy can touch its views.错误的解决方法
- Android 解决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.
- "Only the original thread that created a view hierarchy can touch its views"引发的思考_Handler的使用
- 浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views. .
- 浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.
- 浅析Android中的消息机制-解决:Only the original thread that created a view hierarchy can touch its views.