在Fragment附加到FragmentManager之前,不能执行onGetLayoutInflater()

MHo*_*gge 8 android android-inflate android-fragments

我有时会得到一个Exception并带有以下消息:

在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)
       ...
Run Code Online (Sandbox Code Playgroud)

我们在这里可以看到,我们首先getLayoutInflater()在第1117行调用该方法,然后在第1151行调用该方法.

第一个就是这个:

/**
 * 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;
}
Run Code Online (Sandbox Code Playgroud)

第二个是抛出的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;
}
Run Code Online (Sandbox Code Playgroud)

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

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);
}
Run Code Online (Sandbox Code Playgroud)

Mah*_*her 1

在 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;
    }
Run Code Online (Sandbox Code Playgroud)