英文:
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, "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());
}
</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("ca-app-pub-XXXXXXXXXXXXXXXX/XXXXXXXXXX").//ca-app-pub-3940256099942544/6300978111
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").//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
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
follow this url to make sure you have implemented all steps:
https://developers.google.com/admob/android/quick-start
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论