I want to directly initialize an array of buttons without having to specify variable names. I thought I found the answear here: Array of buttons in Kotlin, but the answer throws a NullPointerException. I have also searched google for 'array of buttons in kotlin' but the only relevant info I found was from the question I linked.
I used
val intervalButtons = arrayOf(
findViewById<Button>(R.id.set30secButton),
findViewById<Button>(R.id.set60secButton),
findViewById<Button>(R.id.set90secButton),
findViewById<Button>(R.id.set120secButton)
)
However I also tried to apply plugin: 'kotlin-android-extensions' in the build.gradle file and just using
val intervalButtons = arrayOf(set30secButton, set60secButton, set90secButton, set120secButton)
but this still throws a NullPointerException.
If I use
btn1 = findViewById<Button>(R.id.set30secButton)
it works like a charm, but like I said, I don't want to have to specify every variable name if I don't have to.
lateinitorlazy, you should init after the view inflated, in activity aftersetContentViewand in fragment inonViewCreated.