androidx.navigation:navigation-fragment:2.2.0,在 navController.popBackStack() 上获取 "You must call removeView() on the child's parent first."
androidx.navigation:navigation-fragment:2.2.0, get "You must call removeView() on the child's parent first." on navController.popBackStack()
我喜欢使用数据绑定和导航图。但是在从 androidx.navigation:navigation-fragment:2.0.0 更新 androidx 依赖项后,2.2.0 应用程序在按下按钮 "Back" 后崩溃。从其他片段到包含 FragmentPagerAdapter.
的前一个片段 return 之后总是崩溃
在build.gradle
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
// This is work
// implementation 'androidx.appcompat:appcompat:1.0.2'
// implementation 'androidx.navigation:navigation-fragment:2.0.0'
// This generate error after backstack
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.navigation:navigation-fragment:2.2.0'
}
如需查看问题和错误,请从 https://github.com/ABRadzh/ErrorNavigation 下载示例。不然我很难解释哪里出错了
- 按任意页面上的任意按钮。
- 按下硬件 "Back" 按钮。
- 获取错误信息:
2020-02-11 16:15:13.119 2429-2429/in.pagerview.navigation.databinding.onbackstack E/AndroidRuntime: FATAL EXCEPTION: main
Process: in.pagerview.navigation.databinding.onbackstack, PID: 2429
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:4954)
at android.view.ViewGroup.addView(ViewGroup.java:4785)
at androidx.viewpager.widget.ViewPager.addView(ViewPager.java:1485)
at android.view.ViewGroup.addView(ViewGroup.java:4725)
at android.view.ViewGroup.addView(ViewGroup.java:4698)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:326)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2625)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2577)
at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2722)
at androidx.fragment.app.FragmentStateManager.activityCreated(FragmentStateManager.java:346)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1188)
at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
at androidx.fragment.app.FragmentManager.run(FragmentManager.java:413)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6719)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:449)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
但是转换片段和数据绑定的所有代码都是自动生成的。我不知道我必须在哪里调用 removeView()。
如果您尝试使用旧的依赖项,请反转代码和布局中的所有注释。 popBackStack() 将正常工作。
我没有发现任何关于这个错误的信息。可能是我做错了什么?
解决方案就是将这段代码添加到每个片段中:
@Override
public void onDestroyView() {
super.onDestroyView();
if(binding.getRoot().getParent() != null)
((ViewGroup)binding.getRoot().getParent()).removeView(binding.getRoot());
}
我真的建议您创建一个包含此代码的 BaseFragment,每个片段都将从它扩展,这样您就不必在每个片段上重复代码 class。
我喜欢使用数据绑定和导航图。但是在从 androidx.navigation:navigation-fragment:2.0.0 更新 androidx 依赖项后,2.2.0 应用程序在按下按钮 "Back" 后崩溃。从其他片段到包含 FragmentPagerAdapter.
的前一个片段 return 之后总是崩溃在build.gradle
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
// This is work
// implementation 'androidx.appcompat:appcompat:1.0.2'
// implementation 'androidx.navigation:navigation-fragment:2.0.0'
// This generate error after backstack
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.navigation:navigation-fragment:2.2.0'
}
如需查看问题和错误,请从 https://github.com/ABRadzh/ErrorNavigation 下载示例。不然我很难解释哪里出错了
- 按任意页面上的任意按钮。
- 按下硬件 "Back" 按钮。
- 获取错误信息:
2020-02-11 16:15:13.119 2429-2429/in.pagerview.navigation.databinding.onbackstack E/AndroidRuntime: FATAL EXCEPTION: main
Process: in.pagerview.navigation.databinding.onbackstack, PID: 2429
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:4954)
at android.view.ViewGroup.addView(ViewGroup.java:4785)
at androidx.viewpager.widget.ViewPager.addView(ViewPager.java:1485)
at android.view.ViewGroup.addView(ViewGroup.java:4725)
at android.view.ViewGroup.addView(ViewGroup.java:4698)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:326)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2625)
at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2577)
at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2722)
at androidx.fragment.app.FragmentStateManager.activityCreated(FragmentStateManager.java:346)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1188)
at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
at androidx.fragment.app.FragmentManager.run(FragmentManager.java:413)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6719)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:449)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
但是转换片段和数据绑定的所有代码都是自动生成的。我不知道我必须在哪里调用 removeView()。
如果您尝试使用旧的依赖项,请反转代码和布局中的所有注释。 popBackStack() 将正常工作。
我没有发现任何关于这个错误的信息。可能是我做错了什么?
解决方案就是将这段代码添加到每个片段中:
@Override
public void onDestroyView() {
super.onDestroyView();
if(binding.getRoot().getParent() != null)
((ViewGroup)binding.getRoot().getParent()).removeView(binding.getRoot());
}
我真的建议您创建一个包含此代码的 BaseFragment,每个片段都将从它扩展,这样您就不必在每个片段上重复代码 class。