非UI线程下更新view会崩溃
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views
private TextView txtContent;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
txtContent = findViewById(R.id.txt_content);
new LooperThread("helloheloo").start();
}
private class LooperThread extends Thread {
private String text;
public LooperThread(String text) {
this.text = text;
}
@Override
public void run() {
super.run();
Thread.currentThread().setName("otherThread");
txtContent.setText(text);
}
}
这样不会崩溃
onCreate内调用setText(),界面不可见,不会调用invalidate方法,更不会调用checkThread(),所以不会崩溃,其实此时的setText方法并没有实现更新view操作。