Google广告无法加载

huangapple go评论84阅读模式
英文:

Google ads are not loading

问题

I am new to this. I have been trying to connect an app to Admob. When I attempt to connect, it always triggers the onAdFailedToLoad() method. This is the same for banner, interstitial and rewarded ads. Please what am I doing wrong?

In the MainActivity, I have:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    MobileAds.initialize(MainActivity.this, "ca-app-pub-XXXXXXXXXXXXXXXX~XXXXXXXXXX");

    //   --- Admob ---
    view = getWindow().getDecorView().getRootView();

    Admob.createLoadBanner(getApplicationContext(), view);
    Admob.loadBanner();
    //   --- *** ---

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

In the Admob class, I have:

public static void createLoadBanner(final Context context, View view) {
    mAdView = view.findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().
            addTestDevice("ca-app-pub-XXXXXXXXXXXXXXXX/XXXXXXXXXX").
            build();
    mAdView.loadAd(adRequest);
    mAdView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            Toast.makeText(context, "Banner Ad has been loaded.", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onAdClosed() {
            super.onAdClosed();
            Toast.makeText(context, "Banner Ad has been closed.", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onAdOpened() {
            super.onAdOpened();
            Toast.makeText(context, "Banner Ad has been opened.", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onAdLeftApplication() {
            super.onAdLeftApplication();
            Toast.makeText(context, "Banner Ad is being redirected.", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            super.onAdFailedToLoad(errorCode);
            Toast.makeText(context, "Banner Ad failed to load!", Toast.LENGTH_LONG).show();
            // loadBanner();
        }
    });
}

public static void loadBanner() {
    if (!mAdView.isLoading()) mAdView.loadAd(new AdRequest.Builder().
            addTestDevice("ca-app-pub-XXXXXXXXXXXXXXXX/XXXXXXXXXX").
            build());
}
英文:

I am new to this. I have been trying to connect an app to Admob. When I attempt to connect, it always triggers the onAdFailedToLoad() method. This is the same for banner, interstitial and rewarded ads. Please what am I doing wrong?

In the MainActivity, I have:

<code>

@Override
public void onCreate(BundleInstanceState bundle) {
super.onCreate(bundle);
MobileAds.initialize(MainActivity.this, &quot;ca-app-pub-XXXXXXXXXXXXXXXX~XXXXXXXXXX&quot;);
//   --- Admob ---
view = getWindow().getDecorView().getRootView();
Admob.createLoadBanner(getApplicationContext(), view);
Admob.loadBanner();
//   --- *** ---
adView = findViewById(R.id.adView);
adView.loadAd(new AdRequest.Builder().build());
}

</code>
<br/>

In the Admob class, I have:

<code>

public static void createLoadBanner(final Context context, View view) {
mAdView = view.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().
addTestDevice(&quot;ca-app-pub-XXXXXXXXXXXXXXXX/XXXXXXXXXX&quot;).//ca-app-pub-3940256099942544/6300978111
build();
mAdView.loadAd(adRequest);
mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
Toast.makeText(context, &quot;Banner Ad has been loaded.&quot;, Toast.LENGTH_LONG).show();
}
@Override
public void onAdClosed() {
super.onAdClosed();
Toast.makeText(context, &quot;Banner Ad has been closed.&quot;, Toast.LENGTH_LONG).show();
}
@Override
public void onAdOpened() {
super.onAdOpened();
Toast.makeText(context, &quot;Banner Ad has been opened.&quot;, Toast.LENGTH_LONG).show();
}
@Override
public void onAdLeftApplication() {
super.onAdLeftApplication();
Toast.makeText(context, &quot;Banner Ad is being redirected.&quot;, Toast.LENGTH_LONG).show();
}
@Override
public void onAdFailedToLoad(int errorCode) {
super.onAdFailedToLoad(errorCode);
Toast.makeText(context, &quot;Banner Ad failed to load!&quot;, Toast.LENGTH_LONG).show();
// loadBanner();
}
});
}
public static void loadBanner() {
if (!mAdView.isLoading()) mAdView.loadAd(new AdRequest.Builder().
addTestDevice(&quot;ca-app-pub-XXXXXXXXXXXXXXXX/XXXXXXXXXX&quot;).//ca-app-pub-3940256099942544/1033173712
build());
}

</code>

答案1

得分: 1

请在清单文件的应用程序标签下添加以下内容:

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

请按照以下网址确保您已经实施了所有步骤:
https://developers.google.com/admob/android/quick-start

英文:

add this in manifest file under application tag

   &lt;meta-data
android:name=&quot;com.google.android.gms.ads.APPLICATION_ID&quot;
android:value=&quot;ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy&quot;/&gt;

follow this url to make sure you have implemented all steps:
https://developers.google.com/admob/android/quick-start

huangapple
  • 本文由 发表于 2020年8月7日 17:01:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63298582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定