【发布时间】:2017-03-15 09:57:54
【问题描述】:
我正在尝试使用包含数据绑定的 adView 布局。但是,它给我一个错误:
java.lang.IllegalStateException:必须在调用 loadAd 之前设置广告尺寸和广告单元 ID。
现在我正在做的是,在我的主要活动布局中,我已经为 adUnitId 包含了带有变量 adId 的布局,如下所示:
<include
android:id="@+id/adViewInclude"
layout="@layout/include_ads"
app:adId="@{@string/main_activity_banner_ad_unit_id}" />
我的include_ads.xml是这样的:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="adId"
type="String" />
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:adSize="SMART_BANNER"
app:adUnitId="@{adId}" />
</FrameLayout>
</layout>
现在在我的 Activity 的 onCreate 方法中,我终于调用了这个方法:
private void loadAds() {
mBinding.executePendingBindings();
AdRequest adRequest = new AdRequest.Builder().build();
mBinding.mainContent.adViewInclude.adView.loadAd(adRequest);
}
但是,它抛出了一个错误。这里出了什么问题?
【问题讨论】:
-
(据我所知)不幸的是,无法绑定
ad ID,原因在您的错误中描述。 (ad unit ID must be set before loadAd is called)。前段时间我遇到了同样的错误,但也许我会尝试找到解决方法:) -
@Amylinn 是的,它只是不起作用。我找到了解决方法。将很快发布代码。
标签: android data-binding admob android-databinding