# CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views

**Code:** `CalledFromWrongThreadException` · **Category:** Android · **Seen on:** Android

Background code updated a View directly; UI changes must happen on the main thread.

## Meaning

Callbacks from network libraries, Executors or Dispatchers.IO run off the main thread. Switch back to Main (withContext(Dispatchers.Main), runOnUiThread, LiveData/StateFlow) before touching views.

## Common causes

- Updating views inside a background callback
- Coroutine running on Dispatchers.IO touching UI
- Timer/Thread callbacks

## Quick fix

1. Use withContext(Dispatchers.Main) or runOnUiThread
2. Expose state via StateFlow/LiveData and observe it in the UI

## Detailed fix by platform

### Kotlin

1. 

```kotlin
lifecycleScope.launch {
    val data = withContext(Dispatchers.IO) { repo.load() }
    textView.text = data.title   // back on Main
}
```


## How to diagnose

1. **Thread** — Callback thread
2. **Code** — Where the view is touched

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/android/calledfromwrongthreadexception)
