メインスレッド以外から画面にsetTextするとCalledFromWrongThreadExceptionが出る件

Android開発でメインスレッド以外から画面にsetTextすると

//backgroundthread

val tvw0 = findViewById<TextView>(R.id.tvw0)
tvw0.setText("test")

以下のエラーが出ます。

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

このエラーはOreo(8.0)以前までで発生しており、 Oreo 以降はエラーにならずbackgroundthreadからでも setText できるようです。

なので、バージョンで分岐させました。

//backgroundthread

val tvw0 = findViewById<TextView>(R.id.tvw0)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {   //26 8.0 以上
    tvw0.setText("test")
}

Follow me!