当前位置: 代码迷 >> Android >> Android学习:UI线程规则+经典错误
  详细解决方案

Android学习:UI线程规则+经典错误

热度:14   发布时间:2016-04-28 02:18:31.0
Android学习:UI线程规则+经典异常

一:看程序



二:可以创建一个新的线程执行阻塞部分
new Thread(new Runnable() {

    @Override
    public void run() {
try {
Thread.sleep(50000);
       } catch (InterruptedException e) {
e.printStackTrace();
       }

}
   });


三:现象
点击button2,不会影像button1,button1继续移动。


四:规则
  There are simply two rules to Android's single thread model:
(1)Do not block the UI thread.不要阻塞UI线程
(2)Do not access the Android UI took it from outside the UI thread.
  不要在UI线程之外的其他线程中,对视图中的组件进行设置。


 经典异常:
 Only the original thread that created a view hierarchy can touch its views.
只有创建view的那个线程才能对其进行修改。


  相关解决方案