1

Okay this is embarrassing but I have been trying to load interstitial ad and it just does not load and I don't understand why even the logcat does not say anything. I have used the same code in two activities and obviously I am using the tester here. So when I click on the RelativeLayout it displays ad. The problem is it is displaying in one activity perfectly with the tester and does not in the other.

Here is what I tried.

    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ad-id");
    mInterstitialAd.loadAd(new AdRequest.Builder().build());
layoutSaved.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
            } else {
                Log.d(TAG, "The interstitial wasn't loaded yet.");
            }
        }
    });

Tried using AdListener to Toast a message to check if it's loaded, and the ad gets loaded but does not open.

P.S: It's worthy to note that I get this error in logcat but everything runs perfectly E/RecyclerView: No adapter attached; skipping layout and I tried these solutions solution 1 and solution 2 but honestly I don't think it will cause any problem (or will it?)

sanjeev
  • 1,664
  • 19
  • 35
  • if you just created the ad recently, you might have to wait a bit, it might not be your fault, probably Google's servers still initializing everything. – grantespo Aug 24 '18 at 17:28
  • @grant Actually I am using the tester ads so it shouldn't be a problem right? However it's loading properly in another activity. – sanjeev Aug 24 '18 at 17:32
  • Tester ads are loaded via Your device's id, not an admob id, so it could be a problem which could be resolved by waiting. So you're saying the admob ad id is loading properly in another activity? – grantespo Aug 24 '18 at 17:34
  • Yup it loads perfectly. I have used banner ads in the same activity. Apparently even that loads. Really been sitting with this for quite sometime now. – sanjeev Aug 24 '18 at 17:51

2 Answers2

0

There are many causes for this problem but Make sure use latest version library version 15.0.1

also set AdListener as below code

interstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            interstitialAd.loadAd(adRequest);
            super.onAdClosed();
        }

        @Override
        public void onAdFailedToLoad(int i) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    interstitialAd.loadAd(adRequest);
                }
            }, 60000);
            super.onAdFailedToLoad(i);
        }
    });

To show the ad use code below

if (interstitialAd != null && interstitialAd.isLoaded()) {
    interstitialAd.show();
} else {
    assert interstitialAd != null;
    interstitialAd.loadAd(adRequest);
}
5ec20ab0
  • 742
  • 6
  • 15
  • To bring to your note am using the latest version, apparently it works on a different activity with same code? Also tried implementing adListener like you said but it doesn't work. – sanjeev Aug 25 '18 at 03:43
0

I can't believe this worked after setting my RecyclerView layout_height to "wrap_content".

So this is what I did in my xml file.

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/toolbar">

I was using the layout click listener and this layout was wrapping the RecyclerView and other views. Since it was set to match_parent the layout click was not recognized as recyclerview was filling the layout although it was empty. I believe this could be the issue. Hope it helps someone in future :)

sanjeev
  • 1,664
  • 19
  • 35