I have a coroutine with an exception handler to call an API. I want to update UI if there is any exception but i got bellow error:
Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
So is there any way to update UI in exception handler?
Here is my code :
val exceptionHandler = CoroutineExceptionHandler { coroutineContext, exception ->
binding.textview.text = exception.message
}
myJob = CoroutineScope(Dispatchers.IO).launch(exceptionHandler) {
val result = webServices.getTest()
withContext(Dispatchers.Main) {
binding.textview.text = "$result"
}
}