Fix Android CalledFromWrongThreadException

In this tutorial, we shall learn to fix Android Exception CalledFromWrongThreadException : Only the original thread that created a view hierarchy can touch its views.

You might have come across this error while from a thread you are trying to set some property of a View or extract some property of a View, that belong to UI thread.

An important point to be remembered while trying to do any operations with UI views is that, only UI thread that created a view hierarchy can touch its views.

Following is simple example, where we try to access a TextView (that belong to UI thread) in another thread.

MainActivity.kt

package com.tutorialkart.runonuithreadexample

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // start some dummy thread that is different from UI thread
        Thread(Runnable {
            // performing some dummy time taking operation
            var i=0;
            while(i<Int.MAX_VALUE){
                i++
            }

            // try to touch View of UI thread
            this.textview_msg.text = "Updated from other Thread"
        }).start()
    }
}

You might see below exception in Logcat

android.view.ViewRootImpl$CalledFromWrongThreadException: 
Only the original thread that created a view hierarchy can touch its views.
     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7534)
     at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1200)
     at android.view.View.requestLayout(View.java:20044)

Following is the line of code that caused this exception

</>
Copy
this.textview_msg.text = "Updated from other Thread"

In general, you touched an UI View from another thread, which you should not do without any help.

To fix this error, wrap the code that has to be executed on UI thread in a Runnable instance passed to runOnUiThread() method.

</>
Copy
this@MainActivity.runOnUiThread(java.lang.Runnable {
    this.textview_msg.text = "Updated from other Thread"
})

Following is the corrected MainActivity.kt

MainActivity.kt

</>
Copy
package com.tutorialkart.runonuithreadexample

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // start some dummy thread that is different from UI thread
        Thread(Runnable {
            // performing some dummy time taking operation
            var i=0;
            while(i<Int.MAX_VALUE){
                i++
            }

            // try to touch View of UI thread
            this@MainActivity.runOnUiThread(java.lang.Runnable {
                this.textview_msg.text = "Updated from other Thread"
            })
        }).start()
    }
}

Conclusion

In this Kotlin Android Tutorial, we have learnt how to fix Android Exception CalledFromWrongThreadException : Only the original thread that created a view hierarchy can touch its views.