I'm making a small Quiz Game and I didn't want to make every function for the Buttons in every Activity, because don't repeat yourself. But when I launch my App it crashes with a Null Pointer Exception.
I tried the Solution at the Bottom and aswell created a function in the ButtonManager class where I had as constructor parameter a String and converted it in the function named above.
This is what my ButtonManager class looks like:
class ButtonManager(buttonName: Int) : AppCompatActivity() {
val button: Button = findViewById(buttonName)
fun quitGame(){
finish()
}
}
This is what my functions call looks like in my Activity:
ButtonManager(R.id.quitGameButton).button.setOnClickListener {
ButtonManager(R.id.quitGameButton).quitGame()
}
This is the Error Code:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference