对于安卓来说UI必须在线程中更新,在子线程中更新会出错。 这个是必然的,因为安卓的UI线程是不安全的。举个例子:多个线程对于同一个textview进行更新,那么最后到底更新出来的是那一个? 我们并不清楚,因为属于不安全线程。
但是今天试了一段代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.text);
imageView = (ImageView)findViewById(R.id.imageview);
Thread t = new Thread(new thread());
t.start();
}
public class thread implements Runnable
{
public void run()
{
textView.setText("qwe");
Log.i("qwe",Thread.currentThread().getName());
}
}
这段代码就是使用子线程去更新UI线程,然而确成功的更新了UI线程并且并没有报错,这是为什么呢?
查了点资料:是这样的当你对一个UI控件进行布局的时候会几步
-> View.invalidate
-> View.invalidateInternal
-> ViewGroup.invalidateChild
-> ViewParent.invalidateChildInParent //这里会不断Loop去取上一个结点的mParent
-> ViewRootImpl.invalidateChildInParent //DecorView的mParent是ViewRootImpl
-> ViewRootImpl.checkThread //在这里执行checkThread,如果非UI线程则抛出异常
但是当Acticity运行到onCreate的时候view tree并没有被初始化,导致了
invalidateChild()函数运行终止不能回调接下来的函数,那么就是无法检测是不是UI主线程了,所以就导致了UI被更新了,但是这样子更新是有很大的危险成分在里面的。如果你把这个线程延时,那么view tree就会被初始化完成,那个时候你在使用,这个就一定会报错.