I have the following function that display a custom toast
fun showCustomToast() {
val inflater = layoutInflater
val container: ViewGroup = findViewById(R.id.toastContainer)
val layout: ViewGroup = inflater.inflate(R.layout.custom_toast, container) as ViewGroup
val text: TextView = layout.findViewById(R.id.textView)
text.text = "This is a custom toast"
with (Toast(applicationContext)) {
setGravity(Gravity.CENTER_VERTICAL, 0, 0)
duration = Toast.LENGTH_LONG
view = layout
show()
}
}
I am calling this function on main activity just below setContentView(R.layout.activity_main)
When running the application I am getting the following error
Caused by: java.lang.IllegalStateException:
> findViewById(R.id.toastContainer) must not be null
What am I doing wrong . . .
I have come across this but it doesn't help