Получение следующей ошибки при попытке загрузить мой NativeAd -
java.lang.IllegalStateException: onGetLayoutInflater() cannot be executed until the Fragment is attached to the FragmentManager.
at androidx.fragment.app.Fragment.getLayoutInflater(Fragment.java:1435)
at androidx.fragment.app.Fragment.onGetLayoutInflater(Fragment.java:1384)
at androidx.fragment.app.Fragment.performGetLayoutInflater(Fragment.java:1416)
at androidx.fragment.app.Fragment.getLayoutInflater(Fragment.java:1401)
at app.meeq.MapFragment$loadAd$1.onUnifiedNativeAdLoaded(MapFragment.kt:683)
MapFragment.kt
class MapFragment : Fragment() {
private fun loadAd() {
val builder = AdLoader.Builder(context, getString(R.string.test_admob_app_unit_id))
builder.forUnifiedNativeAd { unifiedNativeAd ->
/** Populate UnifiedNativeAdView with loaded NativeAd */
val adView = layoutInflater.inflate(R.layout.ad_unified, null) as UnifiedNativeAdView
populateUnifiedNativeAdView(unifiedNativeAd, adView)
}
/** Load ad */
val adLoader = builder
.withAdListener(object : AdListener() {
override fun onAdFailedToLoad(errorCode: Int) {
log("Ad failed to load: $errorCode")
}
}).build()
adLoader.loadAd(AdRequest.Builder().build())
}
ad_unified.xml
<com.google.android.gms.ads.formats.UnifiedNativeAdView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ad_unified">
<TextView style="@style/AdAttribution"/>
<include
android:layout_height="match_parent"
android:layout_width="match_parent"
layout="@layout/fragment_profile">
</include>
</com.google.android.gms.ads.formats.UnifiedNativeAdView>
когда написано onGetLayoutInflater() cannot be executed until the Fragment is attached to the FragmentManager
- на какой фрагмент он ссылается? loadAd()
вызывается в onStart()
фрагмента, поэтому MapFragment
определенно присоединен к диспетчеру фрагментов.
Как обойти эту проблему?