I was experimenting with the AsyncTask and found an unusual scenario, here is my code.
class MainActivity : AppCompatActivity() {
lateinit var textView:TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView = findViewById(R.id.textView)
val task = MyAsyncTask()
task.execute()
}
inner class MyAsyncTask:AsyncTask<Void,Void,String>() {
override fun doInBackground(vararg params: Void?): String {
//Thread.sleep(3000)
textView.text ="From AsyncTask"
return "hello"
}
}
}
It worked!!, see the Thread.sleep is commented but if I uncomment it then the usual error what we know
Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
Why is so, if anybody know please share your feedback. Thanks