Skip to content Skip to sidebar Skip to footer

Nullpointerexception On Button's Onclicklistener

I'm writing an Android game app for an Artificial Intelligence semester project. I'm getting a NullPointerException on Line 38 of the main class, which is the setOnClickListener fo

Solution 1:

The only reason why findViewById() would return null is because the View you are looking for does not belong to the current View. (The View you set with setContentView()). If R.id.buttonNewGame is in R.layout.startscreen, try to clean and rebuild your project.

Solution 2:

It would seem that newGame is null. Do you have multiple layouts named startscreen.xml? A common cause of this exception would be creating buttonNewGame in one version but not the other; this would cause the code to build correctly but fail at runtime, exactly as you're describing, when the active layout failed to specify the button.

Solution 3:

I see this error periodically, the trouble in my case is that android inpropertly maintaining same id's in different layouts. Make sure that your id's are unique not inside only one layout, but in entire application.

Solution 4:

Set a break point on line 38 and launch the debugger. You're most likely trying to reference a button outside of the xml file R.layout.startscreen which would throw a NullPointerException.

Post a Comment for "Nullpointerexception On Button's Onclicklistener"