How to get Google's example code working for AdMob 4.1.0 using an Android device

余生长醉 提交于 2019-12-17 21:29:24

问题


I want to use xml to add a banner ad to my app. To understand how to do this, I read through the Google AdMob Ads Android Banner XML tutorial. There's even example code. However, the example project doesn't display ads on my device (a Motorola xt720). I get nothing relevant in LogCat.

If I go to project settings and remove the 4.0.4 jar and add the 4.1.0 jar that I downloaded with Android SDK and AVD Manager (downloaded "Google Admob Ads Sdk package, revision 2" and the file is android-sdk-linux_86/extras/google/admob_ads_sdk/GoogleAdMobAdsSdk-4.1.0.jar), then I get this in LogCat.

ERROR/Ads(4486): AdView missing required XML attribute "adSize".

INFO/Ads(4486): To get test ads on this device, call adRequest.addTestDevice("00000000000000000000000000000000");

I make that change:

    AdRequest adRequest = new AdRequest();
    adRequest.addTestDevice("00000000000000000000000000000000");
    adView.loadAd(adRequest);

And now I get this:

ERROR/Ads(5018): AdView missing required XML attribute "adSize".

WARN/Ads(5018): Invalid unknown request error: Cannot determine request type. Is your ad unit id correct?

INFO/Ads(5018): onFailedToReceiveAd(Invalid Google Ad request.)

If I edit main.xml to fix the ads:adUnitId to be the value I have in my admob account, I still get the same error. As you can see, I am setting my adSize:

<com.google.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:adUnitId="000000000000000"
    ads:adSize="BANNER" />

So in summary:

  • I fixed the library version
  • I added my test device
  • I set my ads:adUnitId

But I still get AdView missing required XML attribute "adSize"

What am I doing wrong?


回答1:


There are some changes to the way Admob works in version 4.1.0

There's a discussion on Google Groups about this issue and how they haven't updated the documentation.

Google employee Tim posted:

1) Remove attrs.xml (or if you need it for your own custom attributes, remove the parts related to AdViews).

2) Change the namespace in your layout from xmlns:ads="http://schemas.android.com/apk/res/com.your.packagename" to xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"

Once you have made those two changes (along with the other changes you already made), you should now see an ad.



来源:https://stackoverflow.com/questions/6389156/how-to-get-googles-example-code-working-for-admob-4-1-0-using-an-android-device

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!