NullPointerException

android

#1

I get the error: java.lang.NullPointerException when iI try to add Android banner into my Project:
setContentView(new GamePanel(this));
moPubView = (MoPubView) findViewById(R.id.adviewMopub);
moPubView.setAdUnitId(“xxxxxxxxxxxxx”);
moPubView.loadAd();

But is working fine if I replace setContentView(new GamePanel(this));
with setContentView(R.layout.activity_main);

Has anyone any idea how to fix it? Thank you in advance.


#2

Solved! Add RelativeLayout in either Activity_main or Content_main

Add this code in MainActivity.java

setContentView(R.layout.activity_main);
moPubView = (MoPubView) findViewById(R.id.adviewMopub);
moPubView.setAdUnitId(ADMOPUB_ID);
moPubView.loadAd(); gamePanel = new GamePanel(this);
RelativeLayout layout= (RelativeLayout) findViewById(R.id.rl); l
ayout.addView(gamePanel);