gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:04:58 25 4
gpt4 key购买 nike

我有时会收到以下消息的异常:

onGetLayoutInflater() cannot be executed until the Fragment is attached to the FragmentManager

我的完整堆栈跟踪(使用 CompositeAndroid 作为父 fragment ):

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 行调用方法。

第一个是这个:

/**
* 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;
}

他们在这里调用已弃用函数是否正常?而且我认为它不应该抛出 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);
}

最佳答案

对于 future 的读者。就我而言。我在 Api 响应内的 fragment 中使用 getLayoutInflater,当我尝试更改 fragment 时,出现此错误

Fatal Exception: java.lang.IllegalStateException: onGetLayoutInflater() cannot be executed until the Fragment is attached to the FragmentManager.

所以我通过取消 fragment 的 Api 调用 onDestroy 解决了这个问题

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

可能对其他用户有帮助。

关于android - 在 Fragment 附加到 FragmentManager 之前不能执行 onGetLayoutInflater(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50213823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com