データバインディングを使用してadViewにインクルードレイアウトを使用しようとしています。しかし、それは私にエラーをスロー:データバインディングを含むAdViewを含める
<include
android:id="@+id/adViewInclude"
layout="@layout/include_ads"
app:adId="@{@string/main_activity_banner_ad_unit_id}" />
マイ:
java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.
今、私がやっている何をすることは私の主な活動のレイアウトでは、私はこのようなadUnitIdための変数ADIDとレイアウトが含まれている、ありますinclude_ads.xml
はこのようなものです:
onCreate
方法で今
<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>
:
private void loadAds() {
mBinding.executePendingBindings();
AdRequest adRequest = new AdRequest.Builder().build();
mBinding.mainContent.adViewInclude.adView.loadAd(adRequest);
}
ただし、エラーが発生しています。ここで何がうまくいかないの?
を使用する必要があり、原因は自分の誤りに記載されています。 (「loadAdが呼び出される前に広告ユニットIDを設定する必要があります」)。私は以前も同じエラーが発生しましたが、おそらく私は回避策を見つけようとします:) – yennsarah
@Amylinnはい、それはうまくいきません。私は回避策を見つけました。すぐにコードを掲載します。 – kirtan403