Facebook Ads Error : You can't call show() for ad in state LOADING
Asked Answered
V

4

6

I'm integrating Facebook Ads in my app following their official documentation. But when I run my app I get the following exception

java.lang.IllegalStateException: You can't call show() for ad in state LOADING. You can change Integration Error mode by setting AdSettings.setIntegrationErrorMode()
    at com.facebook.ads.internal.c.a.a(Unknown Source:122)
    at com.facebook.ads.internal.c.f.a(Unknown Source:6)
    at com.facebook.ads.InterstitialAd.show(Unknown Source:2)
    at tech.grapegames.pdfreader.ui.MainActivity.moveToFragment(MainActivity.java:115)
    at tech.grapegames.pdfreader.ui.HomeFragment.onClick(HomeFragment.java:85)
    at android.view.View.performClick(View.java:6897)
    at android.view.View$PerformClick.run(View.java:26101)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6944)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

Below is my code

private com.facebook.ads.InterstitialAd fbInterstitialAd;
fbInterstitialAd = new com.facebook.ads.InterstitialAd(this, getString(R.string.fb_interstitial_ad_id));
fbInterstitialAd.loadAd();

and I'm showing it like below

if (fbInterstitialAd.isAdLoaded() && !fbInterstitialAd.isAdInvalidated()) {
fbInterstitialAd.show();//This line causes the crash

This is the dependency that I've added in build.gradle

implementation 'com.facebook.android:audience-network-sdk:5.+'
Vehement answered 22/10, 2018 at 12:57 Comment(0)
A
5

You are using the wrong dependency. Replace the following dependency:

implementation 'com.facebook.android:audience-network-sdk:5.+'

with

implementation 'com.facebook.android:audience-network-sdk:5.0.1'
Attached answered 29/10, 2018 at 7:43 Comment(0)
T
5

Hi: you need to check if your ad is not null and if it is loaded before show it:

if(fbInterstitialAd != null && fbInterstitialAd.isAdLoaded()) fbInterstitialAd.show();

Trackless answered 20/11, 2018 at 0:23 Comment(0)
A
2

I updated the facebook ads from 5.0.1 to 5.3.1 and I have the same error:

java.lang.IllegalStateException: You can't call load() for ad in state LOADING. Previous states: LOADING << CREATED. You can change Integration Error mode by setting AdSettings.setIntegrationErrorMode()

This error happened when you trying to load a new ad when the previous is loading. What did they smoke when writing code? They should redirect the error to "onError" method, but they decide to throw an IllegalStateException

My solution is not elegant, but it is working:

// load the ad
public void load() {
    if (interstitialAd != null) {
        try {
            interstitialAd.loadAd();
        } catch (Throwable e) {
            // Do nothing, just skip and wait for ad loading
        }
    }
}

// show the ad
public void show() {
    if (interstitialAd != null && interstitialAd.isAdLoaded()) {
        try {
            interstitialAd.show();
        } catch (Throwable e) {
            // Do nothing, just skip and wait for ad loading
        }
    }
}
Adkins answered 6/6, 2019 at 13:45 Comment(0)
H
0
 fb_interstitialAd.setAdListener(new InterstitialAdListener() {
        @Override
        public void onInterstitialDisplayed(Ad ad) {
            Toast.makeText(VideoStatusActivity.this, "Display", 
          Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onInterstitialDismissed(Ad ad) {
            fb_interstitialAd.loadAd();
        }

        @Override
        public void onError(Ad ad, AdError adError) {

        }

        @Override
        public void onAdLoaded(Ad ad) {

        }

        @Override
        public void onAdClicked(Ad ad) {

        }

        @Override
        public void onLoggingImpression(Ad ad) {

        }
    });
Hyperesthesia answered 22/4, 2019 at 5:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.