1

I am implementing admob banner ad in the libgdx game. I have made several screens and want to show banner ad on each and every screen. I implemented banner ad in the launcher class and trying to show it in the screen class through the interface method calling, but the application is crashing every time with the error.

4-22 20:47:14.466 24246-24280/com.mygdx.game E/AndroidRuntime: FATAL EXCEPTION: GLThread 80476

Process: com.mygdx.game, PID: 24246
java.lang.NullPointerException: Attempt to invoke interface method 'void com.mygdx.game.AdsController.showBannerAd()' on a null object reference
    at com.mygdx.game.BabyOrange.create(BabyOrange.java:51)
    at com.mygdx.game.BabyScreen.<init>(BabyScreen.java:38)
    at com.mygdx.game.BabyOrange.<init>(BabyOrange.java:42)
    at com.mygdx.game.BabyGame.create(BabyGame.java:9)
    at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:311)
    at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1633)
    at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1354)

Here is the code for the launcher and the screen class

public class BabyOrange extends BabyScreen {

private BabyActor bg;
private BabyActor ph;

private AdsController adsController;

public BabyOrange(Game g,AdsController adsController){
   super(g);
   this.adsController = adsController;


}

 public void create () {
   adsController.showBannerAd();

    bg = new BabyActor();
    bg.setTexture(asset.get("background-orange.png",Texture.class));
    bg.setSize(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());

    ph = new BabyActor();
    ph.setTexture(asset.get("orange-ph.png",Texture.class));
    ph.setSize(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());

 }

Here is the interface

public interface AdsController {
public void showBannerAd();
public void hideBannerAd();
 }

And the Android launcher class

public class AndroidLauncher extends AndroidApplication implements AdsController{

private static final String BANNER_ID = "ca-app-pub-959117648XXXXXX/XXXXXXXX";
AdView bannerAd;
@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();

    //create a gameView and a bannerAd AdView
    View gameView = initializeForView(new BabyGame(), config);

    setupAds();

    //define the layout
    RelativeLayout layout = new RelativeLayout(this);
    layout.addView(gameView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    layout.addView(bannerAd,params);
    setContentView(layout);
}

public void setupAds(){
    bannerAd = new AdView(this);
    bannerAd.setVisibility(View.INVISIBLE);
    bannerAd.setAdUnitId(BANNER_ID);
    bannerAd.setAdSize(AdSize.SMART_BANNER);
}

@Override
public void showBannerAd() {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            bannerAd.setVisibility(View.VISIBLE);
            AdRequest.Builder builder = new AdRequest.Builder();
            AdRequest ad = builder.build();
            bannerAd.loadAd(ad);
        }
    });
}

@Override
public void hideBannerAd() {

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            bannerAd.setVisibility(View.INVISIBLE);
        }
    });
}

}

danish
  • 187
  • 1
  • 1
  • 14
  • 1
    Your `adsController` is null – Bach Vu Apr 23 '19 at 03:12
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – ADM Apr 23 '19 at 04:50
  • I solved the nullPointerEception but the ads are still not showing. – danish Apr 23 '19 at 07:06

1 Answers1

1

Your super constructor calls the underlying create function before your adsController is set. Try this code:

public BabyOrange(Game g,AdsController adsController){
    super(g);
    this.adsController = adsController;
    create2();
}

public void create2() {
    adsController.showBannerAd();
}

public void create () { // called within super-constructor
    bg = new BabyActor();
    bg.setTexture(asset.get("background-orange.png",Texture.class));
    bg.setSize(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());

    ph = new BabyActor();
    ph.setTexture(asset.get("orange-ph.png",Texture.class));
    ph.setSize(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());

}

...
zeg
  • 432
  • 2
  • 9