OSbOoL
OSbOoL

Reputation: 13

Google Admob Ad crashes app while loading interstitial ad

my app crashes while loading Google Admob ad. It worked fine for a long time but now I get following error: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.interstitial.InterstitialAd.setFullScreenContentCallback(com.google.android.gms.ads.FullScreenContentCallback)' on a null object reference

I've changed nothing in code, my app is in Play Store and I have the App on my Galaxy S10 & it worked all the time but now if the Ad has to be loaded I get this error. Can somebody help? Thanks

Edit: Here's my code

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) {
        }
    });

    AdRequest adRequest = new AdRequest.Builder().build();

    InterstitialAd.load(this,"ca-app-pub-93706071examplenumber/4141725063", adRequest, new InterstitialAdLoadCallback() {
        @Override
        public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
           
            mInterstitialAd = interstitialAd;
            Log.i("TAG", "onAdLoaded");
        }

        @Override
        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
            
            Log.i("TAG", "onAdLoaded");
            mInterstitialAd = null;
        }
    });












private void showInterstitial(){

    if (callbackActive == false){
        setCallback();
    }


    if (mInterstitialAd != null ) {
        mInterstitialAd.show(MainActivity.this);
    } else {
        Log.d("TAG", "The interstitial ad wasn't ready yet.");
    }
}


private void setCallback(){
    callbackActive = true;
    mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
        @Override
        public void onAdDismissedFullScreenContent() {
       
            Log.d("TAG", "The ad was dismissed.");

        }

        @Override
        public void onAdFailedToShowFullScreenContent(AdError adError) {
    
            Log.d("TAG", "The ad failed to show.");
        }

        @Override
        public void onAdShowedFullScreenContent() {
      
            mInterstitialAd = null;
            Log.d("TAG", "The ad was shown.");
        }
    });
}

Upvotes: 1

Views: 1108

Answers (1)

user16930239
user16930239

Reputation: 9717

Put setFullScreenContentCallback in onAdLoaded here is a code snippet (see code below) and check the full code from AdMob official sample on GitHub

public void loadAd() {
    AdRequest adRequest = new AdRequest.Builder().build();
    InterstitialAd.load(
        this,
        AD_UNIT_ID,
        adRequest,
        new InterstitialAdLoadCallback() {
          @Override
          public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
            // The mInterstitialAd reference will be null until
            // an ad is loaded.
            MyActivity.this.interstitialAd = interstitialAd;
            Log.i(TAG, "onAdLoaded");
            Toast.makeText(MyActivity.this, "onAdLoaded()", Toast.LENGTH_SHORT).show();
            interstitialAd.setFullScreenContentCallback(
                new FullScreenContentCallback() {
                  @Override
                  public void onAdDismissedFullScreenContent() {
                    // Called when fullscreen content is dismissed.
                    // Make sure to set your reference to null so you don't
                    // show it a second time.
                    MyActivity.this.interstitialAd = null;
                    Log.d("TAG", "The ad was dismissed.");
                  }

                  @Override
                  public void onAdFailedToShowFullScreenContent(AdError adError) {
                    // Called when fullscreen content failed to show.
                    // Make sure to set your reference to null so you don't
                    // show it a second time.
                    MyActivity.this.interstitialAd = null;
                    Log.d("TAG", "The ad failed to show.");
                  }

                  @Override
                  public void onAdShowedFullScreenContent() {
                    // Called when fullscreen content is shown.
                    Log.d("TAG", "The ad was shown.");
                  }
                });
          }

Upvotes: 2

Related Questions