java.lang.IllegalStateException:在将Fragment附加到FragmentManager之前,无法执行onGetLayoutInflater()

附件:

嗨,我正在尝试在Android应用中实施原生广告,因此我有此代码,您可以在Github中检查源代码。

有时会在Logcat中触发此错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.august_themes.free_fast_stock_inventory_manager, PID: 3234
java.lang.IllegalStateException: onGetLayoutInflater() cannot be executed until the Fragment is attached to the FragmentManager.
    at androidx.fragment.app.Fragment.getLayoutInflater(Fragment.java:1503)
    at androidx.fragment.app.Fragment.onGetLayoutInflater(Fragment.java:1452)
    at androidx.fragment.app.Fragment.performGetLayoutInflater(Fragment.java:1484)

执行以下代码时发生错误:

  private void refreshAd() {
    refresh.setEnabled(false);

    AdLoader.Builder builder = new AdLoader.Builder(getContext(), getResources().getString(R.string.AdmobNativeAdsID));

    builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
        // OnUnifiedNativeAdLoadedListener implementation.
        @Override
        public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
            // You must call destroy on old ads when you are done with them,
            // otherwise you will have a memory leak.
            if (nativeAd != null) {
                nativeAd.destroy();
            }
            nativeAd = unifiedNativeAd;
            FrameLayout frameLayout =
                    v.findViewById(R.id.nativeads_adplaceholder);
            UnifiedNativeAdView adView = (UnifiedNativeAdView) getLayoutInflater()
                    .inflate(R.layout.ad_unified, null);
            populateUnifiedNativeAdView(unifiedNativeAd, adView);
            frameLayout.removeAllViews();
            frameLayout.addView(adView);
        }

    });

我们正在谈论这行代码

  UnifiedNativeAdView adView = (UnifiedNativeAdView) getLayoutInflater().inflate(R.layout.ad_unified, null);

因此,我的问题是: 我可以在调用此函数getLayoutInflater()之前测试类似实现条件的条件,以确保将Fragment附加到Fragment Manager吗?

Nima Ganji :

根据Android Developers的问题,.isAdded()如果片段当前已添加到其活动中,则该片段具有返回true的功能。希望能帮助到你。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

TOP 榜单

热门标签

归档