6

//This is my Activity where i want to load my ad

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;


public class TenaliRamanActivity extends AppCompatActivity {

    ListView listView;
    private InterstitialAd mInterstitialAd;
    private AdView mAdView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tenali_raman);

//AD Code Start

        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });

        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                // Code to be executed when an ad finishes loading.
            }

// I am overriding onAdFailedToLoad() method to know why my ad is not showing and getting error code 3, message "no ad config"

            @Override
            public void onAdFailedToLoad(LoadAdError adError) {
                Toast.makeText(TenaliRamanActivity.this, " Banner AdError is "+ adError, 
 Toast.LENGTH_LONG).show();
                
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
            }

            @Override
            public void onAdClicked() {
                // Code to be executed when the user clicks on an ad.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
            }
        });

// Interstitial code Start here

        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-4745680273727033/3318988209");
        mInterstitialAd.loadAd(new AdRequest.Builder().build());

        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                // Code to be executed when an ad finishes loading.
            }

//I also Toasted the my Inetrstitial Ad error message and getting same error code 3, message "no ad config"[my error is this][1]

            **@Override
            public void onAdFailedToLoad(LoadAdError adError) {
                Toast.makeText(TenaliRamanActivity.this, " Instertial Ad Error is"+ adError, 

Toast.LENGTH_SHORT).show(); enter code here}**

            @Override
            public void onAdOpened() {
                // Code to be executed when the ad is displayed.
            }

            @Override
            public void onAdClicked() {
                // Code to be executed when the user clicks on an ad.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when the interstitial ad is closed.
            }
        });


       // Start Application Code
        listView= findViewById(R.id.listView);

        String[] tenaliRaman= getResources().getStringArray(R.array.tenaliRaman);

        ArrayAdapter<String> adapter= new ArrayAdapter<>(this,R.layout.row_item, R.id.rowText, tenaliRaman);

        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mInterstitialAd.isLoaded()) {
                    mInterstitialAd.show();
                } else {
                    Intent intent= new Intent(TenaliRamanActivity.this, StoryTenaliRaman.class);
                    intent.putExtra("storyPosition", position);
                    startActivity(intent);
                }



            }
        });

    }
}


  [1]: https://i.stack.imgur.com/rNFvv.png
kumar bittu
  • 111
  • 2
  • 4
  • Answer all ready given [https://stackoverflow.com/questions/33566485/failed-to-load-ad-3#:~:text=It%20Means%20that%20your%20code,will%20load%20the%20ads%20immediately.] This will solve your problem – Aadesh Mishra Feb 19 '21 at 06:54
  • 1
    But i can't find any working answer.. – kumar bittu Feb 19 '21 at 07:56
  • 1
    @kumarbittu answer is mentioned there if you are getting error 3 on admob means The ad request was successful, but no ad was returned due to lack of ad inventory. your code is correct ad called but no ad in inventory – Usama Altaf Feb 19 '21 at 08:07
  • Does this answer your question? [failed to load ad : 3](https://stackoverflow.com/questions/33566485/failed-to-load-ad-3) – Daan Seuntjens Feb 19 '21 at 16:14
  • 1
    @DaanSeuntjens i got error from more than 15 days it shows same error...whenever i try to load my real ad – kumar bittu Feb 19 '21 at 16:45
  • https://stackoverflow.com/questions/33566485/failed-to-load-ad-3-with-doubleclick This answer is useful to you. – Abhi S Mar 04 '22 at 10:23

2 Answers2

2

For anyone with similar issue, this error also happens intermittently with google's demo ads: https://developers.google.com/admob/android/test-ads#demo_ad_units

In my case I was using the banner demo:

ca-app-pub-3940256099942544/6300978111

And after testing it multiple times it failed 50% of the time. Real ads are working since I can see the stats in admob dashboard, so this should be safe to ignore... I hope.

carrizo
  • 689
  • 6
  • 15
1

First Of All go to project-level build.gradle

allprojects {
    repositories {
        google()
    }
}

instead of google() use this

maven {
    url 'https://maven.google.com/'
    name 'Google'
}

Did you use Testing IDs ?

Now go to your manifest file

Make sure you added the INTERNET permission in your AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

And Also use test admob app id in your AndroidManifest.xml also

 <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-3940256099942544~3347511713"/>

Test AdMob app ID: ca-app-pub-3940256099942544~3347511713

The source for the Test Admob app ID

Then You can also find here the rest of Testing ids

App Open: ca-app-pub-3940256099942544/3419835294

Banner: ca-app-pub-3940256099942544/6300978111

Interstitial: ca-app-pub-3940256099942544/1033173712

Interstitial Video: ca-app-pub-3940256099942544/8691691433

Rewarded: ca-app-pub-3940256099942544/5224354917

Rewarded Interstitial: ca-app-pub-3940256099942544/5354046379

Native Advanced: ca-app-pub-3940256099942544/2247696110

Native Advanced Video: ca-app-pub-3940256099942544/1044960115

replace your ids with these test ids to be able to see your ads in testing mode

AbuNahed
  • 23
  • 5
  • 6
    my test id is working fine it will show ad but when i put my own ad id it will not show any ad and shows error code 3, no ad config – kumar bittu Mar 06 '21 at 03:27
  • If the ads appear and you are using the testing ids numbers, that is mean everything is fine. As for the ads not appearing on the real ids numbers, this is because you must publish the application on the store and then download it on your device after agreeing to publish it, and the ads will appear. – AbuNahed Jul 09 '21 at 22:53