在数据绑定中包含AdView [英] Include AdView with databinding

查看:100
本文介绍了在数据绑定中包含AdView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对包含数据绑定的adView使用包含布局.但是,这给我抛出了一个错误:

I am trying to use include layout for the adView with data binding. But, it throws me an error:

java.lang.IllegalStateException:必须在调用loadAd之前设置广告尺寸和广告单元ID.

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

现在我正在做的是,在我的主要活动布局中,我已经为adUnitId包含了带有变量adId的布局,如下所示:

Now what I am doing is, in my main activity layout, I have included the layout with variable adId for the adUnitId like this:

       <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 方法中,我终于要调用此方法:

Now in my Activity's onCreate method I am calling this method at last:

private void loadAds() {
    mBinding.executePendingBindings();
    AdRequest adRequest = new AdRequest.Builder().build();
    mBinding.mainContent.adViewInclude.adView.loadAd(adRequest);
}

但是,它抛出一个错误.怎么了?

But, it is throwing an error. What's going wrong here?

推荐答案

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

这篇关于在数据绑定中包含AdView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆