我尝试在没有现有活动的情况下创建AdView实例:
https://developer.android.com/reference/com/google/android/gms/ads/AdView.html
AdView(上下文上下文,AttributeSet attrs) //从XML布局构建AdView。
XmlPullParser parser = context.getResources().getXml(R.xml.admob);
AttributeSet attributes = Xml.asAttributeSet(parser);
adView = new AdView(context, attributes);
我尝试过不同类型的XML内容,但我找不到合适的解决方案。
我尝试过的XML的一个例子:
<com.google.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER">
</com.google.ads.AdView>
无论我尝试,都会抛出以下异常:
LogCat: E/AndroidRuntime(29853): Caused by: com.google.ads.internal.b: Required XML attribute "adSize" missing
E/AndroidRuntime(29853): at com.google.ads.AdView.b(SourceFile:467)
E/AndroidRuntime(29853): at com.google.ads.AdView.a(SourceFile:336)
有人可以告诉我如何使用AdView构造函数的这种变体吗?
答案 0 :(得分:1)
我发现在没有Activity的情况下构建AdView要么不可能也不太困难。
但作为“如何为没有活动的环境创建AdView”的解决方案,毕竟不是那么困难。
请执行以下操作:
似乎工作正常(至少Android 4.2)。
答案 1 :(得分:0)
您遇到的错误
Required XML attribute "adSize" missing
是因为您的AdView的XML配置中没有adSize
属性。您需要选择要显示的尺寸广告。
请参阅https://developers.google.com/mobile-ads-sdk/docs/admob/intermediate#android