Here I am trying to add a view over main_layout for a specific time duration, for that I write this
main_layout.addView(linearLayout)
Handler(Looper.getMainLooper()).post(Runnable {
val timer = Timer()
timer.schedule(object : TimerTask() {
override fun run() {
main_layout.removeView(imageView)
timer.cancel()
}
}, 8000)
})
but app gets crash with the error :
java.lang.IllegalStateException: The current thread must have a looper!
kindly let me know, what else I have to do here.
I have resolved my problem by using HandleMessage()
method in handler.
mainHandler = object :Handler(){
override fun handleMessage(msg: Message?) {
super.handleMessage(msg)
if (msg!!.what ==1){
removeView()
}
}
}
private fun removedata() {
main_layout.removeView(imageView)
}
also, to perform the desired operation, this method sends the required sets of instruction
mainHandler.sendEmptyMessageDelayed(1,5000)
so as you can see that to call remove(), it requires 1 to fullfil the condition and second parameter is the desired delay I wanted to set.