android listview 与 一般多线程配合产生的异常,资料汇总
1---------------------------------------------------
在做项目时,偶尔写程序发现一个不经意的混乱事件!!如下代码:
public void onCreate(Bundle savedInstanceState) {
..
..
..
new Thread(new Runnable(){
public void run() {
// TODO Auto-generated method stub
btn_start_audio.setText("Confuse");
}
});
}
这时竟然可以运行成功,而且使button的text修改成功。"Only the original thread that
created a view hierarchy can touch its views.“ 异常。
不过此次更改Button
Text仍然是由UI线程完成的。因为在子线程启动之后,开始Run,而这时Button的Parent,或者是parent
的parent的(父视图的服视图) mParent.isLayoutRequested() 为true。导致子线程无法运行到
ViewRoot的requestLayout(),所以不会抛出上述异常。但是此时Button类里面的mText变量已经被更改了。当CPU切换到
UI线程开始layout整个View 时,同时也就给Button 展现了一个 mText(刚才已经更新)。
new Thread(new Runnable(){
public void run() {
Thread.Sleep(500);
// TODO Auto-generated method stub
btn_start_audio.setText("Confuse");
}
});
}
这样就会出现异常。
大家正在看
|
<script type="text/javascript"><!-- google_ad_client = "pub-1330011834602286"; /* 160x600, 创建于 11-7-20 */ google_ad_slot = "7068865891"; google_ad_width = 160; google_ad_height = 600; //--> </script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script> |
<script type="text/javascript"> /*250*200,创建于2011-8-18*/ var cpro_id = 'u579967';</script><script src="http://cpro.baidu.com/cpro/ui/f.js" type="text/javascript"></script><script src="http://pos.baidu.com/ecom?di=u579967&tm=BAIDU_CPRO_SETJSONADSLOT&fn=BAIDU_CPRO_SETJSONADSLOTFLOAT&baidu_id=" type="text/javascript"></script>