Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What to do about NullPointerException when trying to grab RelativeLayout and add views to it?

 
Eric Sweeten
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to grab the RelativeLayout that's there by default and add one single view to it - an AdView. I'm making a game and otherwise don't deal with Views. This is my code:



It's throwing a NullPointerException as soon as I attempt to access the rLayout. It says:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setLayoutParams(android.view.ViewGroup$LayoutParams)' on a null object reference
 
Paul Clapham
Sheriff
Posts: 21542
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The message is even clearer than NullPointerException messages given by Java: It clearly says that rLayout is null.

And your code assigns the result of the findViewById method to rLayout, so clearly that result is null. So you should look into why app.findViewById() is returning null. Without knowing anything about your application I would guess that the method is looking up views based on an ID, and the ID you passed to the method doesn't have any corresponding view.
 
Eric Sweeten
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The R.id.relativeLayout is referring to the only RelativeLayout in the xml file. Here is my xml file:



I don't understand why it's returning null.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic