[英]Android - Admob test ads not appearing in app
I want to integrate Google Mobile Ads into my app, but when I debug it, whenever using my own device, or using an emulator, no ads appear. 我想将Google Mobile Ads集成到我的应用程序中,但是当我对其进行调试时,无论是使用自己的设备还是使用模拟器,都不会显示任何广告。
I am using the code from Google's developer page: https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals 我正在使用Google开发人员页面上的代码: https : //developers.google.com/mobile-ads-sdk/docs/admob/fundamentals
When looking at logcat, I find that my app is not requesting any ads, and Admob is reporting no requests. 查看logcat时,我发现我的应用未请求任何广告,并且Admob未报告任何请求。
What am I missing? 我想念什么?
Here is my code 这是我的代码
AndroidManifest Android清单
<application android:icon="@drawable/ic_launcher">
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
android:label="@string/app_name" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>
layout - main.xml 布局-main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/adBanner"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads">
<com.google.ads.AdView android:id="@+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="-omitted-"
ads:adSize="BANNER" />
</LinearLayout>
AdBanner.java (the class with Google's example test code) AdBanner.java(带有Google示例测试代码的类)
package com.google.android.gms.ads.banner;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import -omitted-
import android.os.Bundle;
import android.widget.LinearLayout;
import -omitted-
public class AdBanner extends MainActivity{
/**
* A simple {@link Activity} that embeds an AdView.
*/
/** The view to show the ad. */
private AdView adView;
/* Your ad unit id. Replace with your actual ad unit id. */
private static final String AD_UNIT_ID = "-omitted-";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
LinearLayout layout = (LinearLayout) findViewById(R.id.adBanner);
layout.addView(adView);
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("-omitted-")
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
}
@Override
public void onResume() {
super.onResume();
if (adView != null) {
adView.resume();
}
}
@Override
public void onPause() {
if (adView != null) {
adView.pause();
}
super.onPause();
}
/** Called before the activity is destroyed. */
@Override
public void onDestroy() {
// Destroy the AdView.
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}
I have seen something strange in your code: 我在您的代码中看到了一些奇怪的东西:
In your AdBanner.java you use this: 在AdBanner.java中,使用以下命令:
import com.google.android.gms.ads.AdView;
but in your layout you use: 但在您的布局中,您使用:
<com.google.ads.AdView
which corresponds to old Admob, but seems you may want to use com.google.android.gms.ads.AdView
in your layout instead 对应于旧版Admob,但似乎您可能想在布局中使用
com.google.android.gms.ads.AdView
I think you must check the android logs to see if any errors or warnings are given for your admob. 我认为您必须检查android日志,看看是否为您的admob提供了任何错误或警告。 For example: I was not able to see the ads too and when i check the logs i found the "not enough space" warning for my advertisement.
例如:我也看不到广告,当我检查日志时,发现广告的“空间不足”警告。 I was using the size "SMART_BANNER" that uses the width as 360dp (you can check the sizes here ).
我使用的尺寸为“ SMART_BANNER”,宽度为360dp(您可以在此处检查尺寸)。 So i changed my AdView's width to exact 360dp to solve my problem and it is now working: Stil
所以我改变了我的AdView的宽度精确360dp解决我的问题,它是现在的工作: 史迪威
I hope this information helps. 我希望这个信息帮助。 Just check the logs and also console.
只需检查日志并进行控制台。
In addition, my adView: 另外,我的adView:
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="360dp"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="@string/banner_ad_unit_id" />
Best regards. 最好的祝福。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.