CalledFromWrongThreadException Android Origin: On your device OSI layer 7 · Application Last updated 13 Oct 2026

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

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

Exact message you may see Only the original thread that created a view hierarchy can touch its views
Seen on: Android

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

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: On your device. Errors that are often confused with or caused by this one (click to open):

An unhandled error has occurred — Blazor: An unhandled error has occurred. Reload 🗙 (.NET)An unhandled error ha…Android resource linking failed — AAPT: error: resource x (aka y) not found — Android resource linking failed (Android)Android resource link…android:attr/lStar not found — AAPT: error: resource android:attr/lStar not found (Android)android:attr/lStar no…ANR — ANR: Application Not Responding (Input dispatching timed out) (Android)ANRBadTokenException — WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? (Android)BadTokenExceptionCannot access database on the main thread — Room: Cannot access database on the main thread since it may potentially lock the UI for a long period of time (Android)Cannot access databas…Main Thread Checker — Main Thread Checker: UI API called on a background thread: -[UIView setNeedsLayout] (iOS)Main Thread CheckerNetworkOnMainThreadException — android.os.NetworkOnMainThreadException (Android)NetworkOnMainThreadEx…CalledFro…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.