80,351
社区成员
发帖
与我相关
我的任务
分享
void checkThread() {
if (mThread != Thread.currentThread()) {
throw new CalledFromWrongThreadException(
"Only the original thread that created a view hierarchy can touch its views.");
}
}
ViewRootIml是接口ViewParent的实现,它会在requestFitSystemWindows()、requestLayout()、invalidateChildInParent(int[] location, Rect dirty)、requestTransparentRegion(View child)、requestChildFocus(View child, View focused)、clearChildFocus(View child)、focusableViewAvailable(View v)、recomputeViewAttributes(View child)、playSoundEffect(int effectId)、focusSearch(View focused, int direction)、doDie()方法中进行检查的
View中大部分方法都是基于ViewParent(指向ViewRootIml的引用),这个检测机制在某个特定的条件下是不会检查的~package com.hundsun.zhoujl.android;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
public class TestUIChangeActivityActivity extends Activity {
private TextView tv = null;
private Handler handler = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.tv);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
tv.setText("Hello Handler");
}
};
Thread thread = new Thread() {
@Override
public void run() {
super.run();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(0);
}
};
thread.start();
}
}