活动XML
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_home_footer">
</com.google.android.gms.ads.AdView>
活动代码
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.build();
mAdView.loadAd(adRequest);
@Override
public void onPause() {
if (mAdView != null) {
mAdView.pause();
}
super.onPause();
}
@Override
public void onResume() {
super.onResume();
if (mAdView != null) {
mAdView.resume();
}
}
@Override
public void onDestroy() {
if (mAdView != null) {
mAdView.destroy();
}
super.onDestroy();
}
构建Grandle代码(模块应用程序)
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.google.android.gms:play-services-ads:9.0.0'
}
当我在我的设备上运行时,广告横幅没有显示,只是一个空白区域 并显示E / Ads:JS:Uncaught ReferenceError:未定义AFMA_ReceiveMessage(:1)和E / libEGL:validate_display:255错误3008(EGL_BAD_DISPLAY)。
问题是什么?
答案 0 :(得分:0)
我在我的项目中使用了这个代码并且工作正常。所以你可以根据你的项目要求进行更改。我希望它对你有用。您可以更改横幅广告单元代码。
活动XML代码
<com.google.android.gms.ads.AdView
android:id="@+id/adView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_5"
android:layout_below="@+id/adView4"
android:layout_alignParentStart="true"
android:layout_marginTop="39dp">
</com.google.android.gms.ads.AdView>
活动代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView mAdView5 = (AdView) findViewById(R.id.adView5);
AdRequest adRequest5 = new AdRequest.Builder().build();
mAdView5.loadAd(adRequest5);
}
Build.Grandle(模块应用)代码
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.gms:play-services-ads:8.4.0'
compile 'com.android.support:design:22.2.0'
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.amazon.android:mobile-ads:5.+'
}
strings.xml代码
<string name="banner_ad_unit_5">ca-app-pub-############/#########</string>