Android Fragments 在将Fragment附加到FragmentManager之前,无法执行onGetLayoutInflater()

9gm1akwq  于 11个月前  发布在  Android
关注(0)|答案(4)|浏览(189)

我有时会得到一个带有以下消息的异常:
在将Fragment附加到FragmentManager之前,无法执行onGetLayoutInflater()
我的完整堆栈跟踪(使用CompositeAndroid作为父片段):

Fatal Exception: java.lang.IllegalStateException: onGetLayoutInflater() cannot be executed until the Fragment is attached to the FragmentManager.
       at android.support.v4.app.Fragment.getLayoutInflater(Fragment.java:1151)
       at com.pascalwelsch.compositeandroid.fragment.CompositeFragment.super_getLayoutInflater(CompositeFragment.java:1310)
       at com.pascalwelsch.compositeandroid.fragment.FragmentDelegate$7.call(FragmentDelegate.java:204)
       at com.pascalwelsch.compositeandroid.fragment.FragmentDelegate$7.call(FragmentDelegate.java:197)
       at com.pascalwelsch.compositeandroid.fragment.FragmentPlugin.getLayoutInflater(FragmentPlugin.java:149)
       at com.pascalwelsch.compositeandroid.fragment.FragmentPlugin.getLayoutInflater(FragmentPlugin.java:1269)
       at com.pascalwelsch.compositeandroid.fragment.FragmentDelegate$7.call(FragmentDelegate.java:202)
       at com.pascalwelsch.compositeandroid.fragment.FragmentDelegate$7.call(FragmentDelegate.java:197)
       at com.pascalwelsch.compositeandroid.fragment.FragmentDelegate.getLayoutInflater(FragmentDelegate.java:208)
       at com.pascalwelsch.compositeandroid.fragment.CompositeFragment.getLayoutInflater(CompositeFragment.java:163)
       at android.support.v4.app.Fragment.onGetLayoutInflater(Fragment.java:1101)
       at com.pascalwelsch.compositeandroid.fragment.CompositeFragment.super_onGetLayoutInflater(CompositeFragment.java:1710)
       at com.pascalwelsch.compositeandroid.fragment.FragmentDelegate$32.call(FragmentDelegate.java:769)
       at com.pascalwelsch.compositeandroid.fragment.FragmentDelegate$32.call(FragmentDelegate.java:762)
       at com.pascalwelsch.compositeandroid.fragment.FragmentPlugin.onGetLayoutInflater(FragmentPlugin.java:528)
       at com.pascalwelsch.compositeandroid.fragment.FragmentPlugin.onGetLayoutInflater(FragmentPlugin.java:1456)
       at com.pascalwelsch.compositeandroid.fragment.FragmentDelegate$32.call(FragmentDelegate.java:767)
       at com.pascalwelsch.compositeandroid.fragment.FragmentDelegate$32.call(FragmentDelegate.java:762)
       at com.pascalwelsch.compositeandroid.fragment.FragmentDelegate.onGetLayoutInflater(FragmentDelegate.java:773)
       at com.pascalwelsch.compositeandroid.fragment.CompositeFragment.onGetLayoutInflater(CompositeFragment.java:538)
       at android.support.v4.app.Fragment.performGetLayoutInflater(Fragment.java:1132)
       at android.support.v4.app.Fragment.getLayoutInflater(Fragment.java:1117)
       at com.my.app.features.event.EventDetailFragment.attachHeader(EventDetailFragment.java:66)
       ...

字符串
我们可以在这里看到,我们首先在第1117行调用方法getLayoutInflater(),然后在第1151行调用方法getLayoutInflater()
第一个是这个:

/**
 * Returns the cached LayoutInflater used to inflate Views of this Fragment. If
 * {@link #onGetLayoutInflater(Bundle)} has not been called {@link #onGetLayoutInflater(Bundle)}
 * will be called with a {@code null} argument and that value will be cached.
 * <p>
 * The cached LayoutInflater will be replaced immediately prior to
 * {@link #onCreateView(LayoutInflater, ViewGroup, Bundle)} and cleared immediately after
 * {@link #onDetach()}.
 *
 * @return The LayoutInflater used to inflate Views of this Fragment.
 */
public final LayoutInflater getLayoutInflater() {
    if (mLayoutInflater == null) {
        return performGetLayoutInflater(null);
    }
    return mLayoutInflater;
}


第二个是抛出Exception的,标记为deprecated

/**
 * Override {@link #onGetLayoutInflater(Bundle)} when you need to change the
 * LayoutInflater or call {@link #getLayoutInflater()} when you want to
 * retrieve the current LayoutInflater.
 *
 * @hide
 * @deprecated Override {@link #onGetLayoutInflater(Bundle)} or call
 * {@link #getLayoutInflater()} instead of this method.
 */
@Deprecated
@NonNull
@RestrictTo(LIBRARY_GROUP)
public LayoutInflater getLayoutInflater(@Nullable Bundle savedFragmentState) {
    if (mHost == null) {
        throw new IllegalStateException("onGetLayoutInflater() cannot be executed until the "
                + "Fragment is attached to the FragmentManager.");
    }
    LayoutInflater result = mHost.onGetLayoutInflater();
    getChildFragmentManager(); // Init if needed; use raw implementation below.
    LayoutInflaterCompat.setFactory2(result, mChildFragmentManager.getLayoutInflaterFactory());
    return result;
}


这里是对deprecated函数的调用,这正常吗?我认为它不应该抛出Exception,因为我在调用getLayoutInflater()之前检查了isAdded()

private void init () {
    if(isAdded()) {
        attachHeader();
        updateHeader();
    }
}

private void attachHeader() {
    headerBinding = DataBindingUtil.inflate(getLayoutInflater(), layout.event_detail_header,
            binding.formsContainer, false);
    binding.formsContainer.addView(headerBinding.getRoot(), 0);
}

0sgqnhkj

0sgqnhkj1#

对于未来的读者。在我的例子中。我在API响应内的片段中使用getLayoutInflater,当我试图更改片段时,我得到了这个错误
严重异常错误:java.lang.IllegalStateException:无法执行onGetLayoutInflater(),直到将片段附加到片段管理器。
因此,我通过取消fragment的API调用onDestroy来解决此问题

@Override
    public void onDestroy() {
        super.onDestroy();
        disposable.dispose(); //RxJava
    }

字符串
可能对其他用户有帮助。

s3fp2yjn

s3fp2yjn2#

在mHost.onGetLayoutInflater()使用此方法检查片段是否已附加之前,您必须首先确保片段已附加到FragmentManager:

Activity activity = getActivity();
    if (isAdded() && activity != null){ // Check the fragment status
        LayoutInflater result = mHost.onGetLayoutInflater();
        getChildFragmentManager(); // Init if needed; use raw implementation below.
        LayoutInflaterCompat.setFactory2(result, 
        mChildFragmentManager.getLayoutInflaterFactory());
    }else{
        LayoutInflater result = null;
    }

字符串

9rbhqvlz

9rbhqvlz3#

我觉得这仍然可以帮助一些人。在绑定视图时从onfoot View使用inflater似乎可以解决这个错误-Fatal Exception:java.lang.IllegalStateException:onGetLayoutInflater()无法执行,直到Fragment附加到FragmentManager

@Override
            public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            render(inflater);
        }
    
    private void render(LayoutInflater inflater) {
    GenericViewBinding genericViewBinding =  GenericViewBinding .inflate(inflater);
    }

字符串
在我的例子中,这个错误发生在(使用默认打开离线缓存的Google firestore,这使得响应感觉是即时的)**documentReference. addSnapshotList(new EventList(){ }**当我试图在我的片段尚未附加到FragmentManager时增加视图时。
我谦卑地写信给你,请不要见怪。我只是想帮助你。也许还有什么我不明白的。我仍然很想听到你的消息。谢谢。

ryevplcw

ryevplcw4#

使用以下代码检查Fragment在使用之前是否已被销毁:
Kotlin:

if (
    activity?.isFinishing == true ||
    activity?.isChangingConfigurations == true || 
    this.isRemoving || this.isDetached || !this.isAdded || 
    this.activity == null || this.view == null) {
    return
} else {
    Todo()
}

字符串

相关问题