`

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"> /*728*90,创建于2011-8-18*/ var cpro_id = 'u579978'; </script><script src="http://cpro.baidu.com/cpro/ui/c.js" type="text/javascript"></script><script src="http://pos.baidu.com/ecom?di=u579978&amp;tm=BAIDU_CPRO_SETJSONADSLOT&amp;fn=BAIDU_CPRO_SETJSONADSLOT&amp;baidu_id=" type="text/javascript"></script>
-
<script type="text/javascript">&lt;!-- google_ad_client = &quot;pub-1330011834602286&quot;; /* 160x600, 创建于 11-7-20 */ google_ad_slot = &quot;7068865891&quot;; google_ad_width = 160; google_ad_height = 600; //--&gt; </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&amp;tm=BAIDU_CPRO_SETJSONADSLOT&amp;fn=BAIDU_CPRO_SETJSONADSLOTFLOAT&amp;baidu_id=" type="text/javascript"></script>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics