【问题标题】:Include AdView with databinding在数据绑定中包含 AdView
【发布时间】: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


【解决方案1】:

您应该使用app:adSize="BANNER" 而不是SMART_BANNER

【讨论】:

  • SMART_BANNER 也是广告尺寸的一部分
  • 那么,mBinding 对象呢?我尝试通过 findViewById() 添加 adView,它对我来说效果很好(使用 SMART_BANNER 参数)
  • 是的.. findViewById 也适合我。我认为数据绑定存在命名空间冲突..
猜你喜欢
  • 2016-02-06
  • 2012-09-27
  • 2015-12-18
  • 1970-01-01
  • 2016-03-21
  • 2010-11-10
  • 2016-01-02
  • 2017-01-19
  • 2018-06-07
相关资源
最近更新 更多