android.view.viewroot$calledfromwrongthreadexception: only the original thread that created a view h
来源:互联网 发布:大尺度网络剧 编辑:程序博客网 时间:2024/06/16 02:13
在android平台下,进行多线程编程时,经常需要在主线程之外的一个单独的线程中进行某些处理,然后更新用户界面显示。但是,在主线线程之外的线程中直接更新页面显示的问题是:系统会报这个异常,android.view.viewroot$calledfromwrongthreadexception: only the original thread that created a view hierarchy can touch its views. (只有原始创建这个视图层次(view hierachy)的线程才能修改它的视图(view)。)。
也就是说必须在程序的主线程(也就是ui线程)中进行更新界面显示的工作。可以采用下面的方法之一来解决:
解决方案1:在activity.oncreate(bundle savedinstancestate)中创建一个handler类的实例, 在这个handler实例的handlemessage回调函数中调用更新界面显示的函数。例如:
- public class ExampleActivity extends Activity {
- Handler h = null;
- @override
- public void onCreate(Bundle savedinstancestate){
- h = new Handler(){
- @override
- public void handleMessage(Message msg){
- // call update gui method.
- }
- };
- }
- }
在其它的函数中,利用 send族或post族函数向这个h发送或邮寄消息即可。
解决方案2:利用activity.runonuithread(runnable)
把更新ui的代码创建在runnable中,然后在需要更新ui时,把这个runnable对象传给activity.runonuithread(runnable)。 这样runnable对像就能在ui程序中被调用。
0 0
- android.view.viewroot$calledfromwrongthreadexception: only the original thread that created a view h
- android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view
- CalledFromWrongThreadException: Only the original thread that created a view
- CalledFromWrongThreadException: Only the original thread that created a view
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a vi
- android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a v
- CalledFromWrongThreadException:Only the original thread that created a view hierarchy can touch its
- ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy
- only the original thread that created a view
- 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
- uva--216+dfs
- Android 百度地图开发(一)--- 申请API Key和在项目中显示百度地图
- 基本概念总结
- Android Sqlite简单增删查改
- 运算优先级、结合性、求值顺序、副作用和顺序点
- android.view.viewroot$calledfromwrongthreadexception: only the original thread that created a view h
- 验证码
- SQLITE数据表主键设置Id自增方法
- cocos2d-x速度效率问题
- Linux连接脚本详解
- 【BZOJ】【P3053】【The Closest M Points】【题解】【kdtree】
- C&C++常见面试题之strcpy
- 常用JS函数
- 模拟赛 牛宫