新的 admob Express Native 广告因 IllegalStateException 而失败

New admob Express Native ads fail with IllegalStateException

我一直在尝试将快速原生广告集成到我的应用程序中。在 express native ads 文档中,我读到它们在以 FULL_WIDTH 广告尺寸显示时效果最佳。 我尝试将我的广告尺寸设置为 FULL_WIDTH,但失败并显示 IllegalStateException:

Caused by: java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.

这是我的 xml 代码:

<com.google.android.gms.ads.NativeExpressAdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="FULL_WIDTH"
ads:adUnitId="@string/native_ad_unit"/>

广告单元正确,适用于 320x150 等其他广告尺寸

我的实现有问题吗?

干杯

您只能通过编程方式进行设置。以下是文档的摘录:

发布商还可以在以编程方式为 NativeExpressAdView 创建 AdSize 时使用 FULL_WIDTH 常量。在这种情况下,广告将占据设备屏幕的整个宽度。

来源:https://firebase.google.com/docs/admob/android/native-express#nativeexpressadview

我找到了一个解决方案,如果你这样使用它就可以了:

<com.google.android.gms.ads.NativeExpressAdView
 android:id="@+id/adView"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 ads:adSize="FULL_WIDTHx80"
 ads:adUnitId="@string/native_ad_unit"/>