밍쯔와 안작고 안귀여운 에러들🖤

[Android/Kotlin]Only the original thread that created a view hierarchy can touch its views. 본문

Develop/Android | iOS

[Android/Kotlin]Only the original thread that created a view hierarchy can touch its views.

밍쯔 2022. 8. 29. 12:45

[오류 코드]

메인 쓰레드가 아닌 곳에서 데이터 바인딩을 시도해서 오류가 발생함.

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.viewModel = MainViewModel()
        binding.activity = this
        
        ...

        Timer().scheduleAtFixedRate(60000,60000) {
           initUser()
        }
    }

 

[해결]

runOnUiThread{} 안에서 실행시켜주면 됨.

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.viewModel = MainViewModel()
        binding.activity = this
        
        ...

        Timer().scheduleAtFixedRate(60000,60000) {
            runOnUiThread {
                initUser()
            }
        }
    }