-1
  • I try execute function inside Thread but that function not executed:

    Thread {
        run {
            Thread.sleep(1000)
            createView(view, inflater)
        }
    }.start()
    
  • So i try with another code like:

    thread(start = true) {
        Thread.sleep(1000)
        createView(view, inflater)
    }
    

    and again the createView not executed

  • Finally I try:

    Thread {
        Thread.sleep(5000)
        fun run() {
            Runnable {
                createView(view, inflater)
            }
        }
    }.start()
    

    and i got an error:

    "Only the original thread that created a view hierarchy can touch its views."

atline
  • 23,297
  • 15
  • 58
  • 86
  • 4
    Possible duplicate of [Android "Only the original thread that created a view hierarchy can touch its views."](https://stackoverflow.com/questions/5161951/android-only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-vi) – Zoe stands with Ukraine Nov 25 '18 at 20:22

1 Answers1

0

Only the Main Thread can alter views. So you would need to do something like this:

Thread {
    Thread.sleep(5000)

    this@SomeActivity.runOnUiThread(Runnable {
        createView(view, inflater)
    })
}.start()
iFanie
  • 796
  • 7
  • 10