为什么新线程可以触摸视图

时间:2019-02-08 14:18:48

标签: android multithreading runnable thread-exceptions android-threading

我们在Android应用程序中知道,我们可以仅从Main/UI Thread触摸视图,否则将抛出CalledFromWrongThreadException并显示以下消息:

android.view.ViewRootImpl$CalledFromWrongThreadException:
Only the original thread that created a view hierarchy can touch its views.

但是当我仅在onCreate()触摸1次时,不会抛出此异常,并且会再次“您好!”在TextView

上设置
 TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = findViewById(R.id.tv);

    new Thread(new Runnable() {
        @Override
        public void run() {
            tv.setText("Hello Again!");
        }
    }).start();
}

你知道为什么吗?

0 个答案:

没有答案