我将尝试使用AdMob在屏幕底部显示广告。
我在build.gradle文件中添加了以下行以获取AdMob sdk:
compile 'com.google.android.gms:play-services-ads:8.4.0'
接下来,我在XML中添加了AdView来展示广告:
<com.google.android.gms.ads.AdView
android:id="@+id/ad_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
app:adSize="BANNER"
android:layout_gravity="bottom"
app:adUnitId="@string/banner_ad_unit_id" />
现在我使用ad_view id引用AdView,并使用以下代码构建AdRequest请求广告:
AdView mAdView = (AdView) findViewById(R.id.ad_view);
final AdRequest adRequest = new AdRequest.Builder()
// .addTestDevice("38C0F116926519A0CBAF1094898921FB")
.build();
if (mAdView != null)
mAdView.loadAd(adRequest);
如果我取消注释addTestDevice
函数,一切都能很好地运行,但是当我尝试显示真正的广告时,在logcat中会出现以下错误:
12-23 17:18:02.234 13226-13226/com.abc.xyz I/Ads: Starting ad request.
12-23 17:18:02.234 13226-13226/com.abc.xyz I/Ads: Use AdRequest.Builder.addTestDevice("38C0F116926519A0CBAF1094898921FB") to get test ads on this device.
12-23 17:18:02.554 13226-13226/com.abc.xyz I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@42dd86b8 time:83516183
12-23 17:18:05.694 13226-13226/com.abc.xyz D/EgretLoader: EgretLoader(Context context)
12-23 17:18:05.694 13226-13226/com.abc.xyz D/EgretLoader: The context is not activity
12-23 17:18:05.764 13226-13226/com.abc.xyz I/Ads: Ad finished loading.
12-23 17:18:05.824 13226-13226/com.abc.xyz E/Ads: JS: Uncaught ReferenceError: AFMA_ReceiveMessage is not defined (:1)
12-23 17:18:05.824 13226-13226/com.abc.xyz I/chromium: [INFO:CONSOLE(1)] "Uncaught ReferenceError: AFMA_ReceiveMessage is not defined", source: (1)
在搜索了相关问题之后,我尝试了以下链接中的解决方案,但是它们都没有对我起作用:Uncaught ReferenceError: AFMA_getSdkConstants is not defined和Interstitial Admob doesn't work : AFMA_ReceiveMessage is not defined。请指导我做错了什么或者遗漏了什么。谢谢。