在回调时返回 viewpager 片段时出现此错误您必须先在 child 的 parent 上调用 removeView()

on callingback the viewpager fragment on back press got this error You must call removeView() on the child's parent first

我正在用 Fragment2 替换其 child Fragment1 中的 Fragment1(它有一个视图寻呼机)。

 Fragment fragment = Childfragment1.this.getParentFragment();
    FragmentManager fragmentManager = fragment.getFragmentManager();

    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    Fragment2 Fragment2 = new Fragment2();


    fragmentTransaction.replace(R.id.cat_container, Fragment2 , "Fragment2 ");
    fragmentTransaction.addToBackStack("Fragment2 ");
    fragmentTransaction.commit();

它工作正常但是当按下后退按钮时它崩溃并出现错误

The specified child already has a parent. You must call removeView() on the child's parent first on super.onBackPressed(); line

@Override
public void onBackPressed() {


    int count = getFragmentManager().getBackStackEntryCount();


    if (count == 0) {


        super.onBackPressed();


    } else {


        getFragmentManager().popBackStack();

    }




}

这是我的 logcat 错误

 java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    at android.view.ViewGroup.addViewInner(ViewGroup.java:5038)
    at android.view.ViewGroup.addView(ViewGroup.java:4869)
    at androidx.viewpager.widget.ViewPager.addView(ViewPager.java:1485)
    at android.view.ViewGroup.addView(ViewGroup.java:4809)
    at android.view.ViewGroup.addView(ViewGroup.java:4782)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:887)
    at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
    at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659)
    at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613)
    at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2624)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:904)
    at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
    at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
    at androidx.fragment.app.BackStackRecord.executePopOps(BackStackRecord.java:500)
    at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2076)
    at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
    at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
    at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManagerImpl.java:310)
    at androidx.fragment.app.FragmentManagerImpl.popBackStackImmediate(FragmentManagerImpl.java:253)
    at androidx.fragment.app.FragmentManagerImpl.handleOnBackPressed(FragmentManagerImpl.java:233)
    at androidx.fragment.app.FragmentManagerImpl.handleOnBackPressed(FragmentManagerImpl.java:108)
    at androidx.activity.OnBackPressedDispatcher.onBackPressed(OnBackPressedDispatcher.java:189)
    at androidx.activity.ComponentActivity.onBackPressed(ComponentActivity.java:286)
    at in.arkemtech.offersoffer.main_freg.Main2Activity.onBackPressed(Main2Activity.java:466)
    at android.app.Activity.onKeyUp(Activity.java:3147)
    at android.view.KeyEvent.dispatch(KeyEvent.java:2744)
    at android.app.Activity.dispatchKeyEvent(Activity.java:3430)
    at androidx.core.app.ComponentActivity.superDispatchKeyEvent(ComponentActivity.java:115)
    at androidx.core.view.KeyEventDispatcher.dispatchKeyEvent(KeyEventDispatcher.java:84)
    at androidx.core.app.ComponentActivity.dispatchKeyEvent(ComponentActivity.java:133)
    at androidx.appcompat.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:558)
    at androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
    at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.dispatchKeyEvent(AppCompatDelegateImpl.java:2814)
    at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:346)
    at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:5213)
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5081)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4600)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4653)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4619)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4759)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4627)
    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4816)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4600)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4653)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4619)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:

您应该使用 getParentFragmentManager (or even getActivity().getSupportFragmentManager()) 获得此交易的 FragmentManager

这是一个更新的答案,正如我预测的那样,另一个修复导致了一个敲门问题:

这是在我将 com.google.android.material 从 1.1.0-alpha05 升级到 1.2.0-alpha04 时在以前工作的应用程序中触发的,一些实验显示 1.1.0-alpha07 以上的任何问题。因此,如果您不喜欢 alpha 版本,请尝试恢复到此版本或更早版本

implementation 'com.google.android.material:material:1.1.0-alpha07'

我仍然不确定这是否真的是 com.google.android.material 中的错误,或者升级后的库是否发现了我代码中的潜在缺陷。


下面是旧答案,因为它可能仍会为某人提供有用的线索:

在我的 fragments onCreateView 中,我将存储创建的视图

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    if (searchView == null) {
        searchView = super.onCreateView(inflater, container, savedInstanceState)
        ...
    }
    return searchView
}

我删除了执行此操作的代码,这解决了问题。

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val searchView = super.onCreateView(inflater, container, savedInstanceState)
    ...
    return searchView
}

我现在正在努力记住为什么我一开始就把视图藏起来了,当然我很快就会发现我的应用程序中的其他东西现在坏了。