当试图加载我的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
肯定附加到片段管理器。
我该如何解决这个问题?
发布于 2019-11-09 07:21:25
您还没有创建片段视图。添加这两个重写方法:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_layout, container, false)
}
然后在这里调用函数loadad()方法:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
loadAd()
}
https://stackoverflow.com/questions/58775564
复制相似问题