我使用 recyclerview 将 viewpager 与导航组件共享的转换分段,它工作正常,但是当我在显示详细信息片段时非常快速地使用后退按钮时,我得到了这个奇怪的崩溃。
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:4326)
at android.view.ViewGroup.addView(ViewGroup.java:4162)
at androidx.fragment.app.FragmentContainerView.addView(FragmentContainerView.java:280)
at android.view.ViewGroup.addView(ViewGroup.java:4103)
at android.view.ViewGroup.addView(ViewGroup.java:4076)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:326)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
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$4.run(FragmentManager.java:413)
at android.os.Handler.handleCallback(Handler.java:743)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5621)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:684)
这是一些android错误还是我做错了什么?花了两天时间试图调试它,似乎没有任何修复它。
public class EntityInfoFragment extends Fragment {
private FragmentEntityInfoBinding binding;
@Override
public void onAttach(@NonNull Context context) {
super.onAttach(context);
OnBackPressedCallback callback = new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
setEnabled(false);
remove();
onBackPressed();
}
};
requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
}
@Override
public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
binding = FragmentEntityInfoBinding.inflate(inflater, container, false);
...
binding.entityInfoViewPager.setAdapter(new ImagePagerAdapter(this, imageUrls));
binding.entityInfoViewPager.setCurrentItem(0);
binding.entityInfoTabLayout.setupWithViewPager(binding.entityInfoViewPager, true);
prepareSharedElementTransition();
if(savedInstanceState == null)
postponeEnterTransition();
return binding.getRoot();
}
@Override
public void onDestroyView() {
if(binding.getRoot().getParent() != null) {
((ViewGroup)binding.getRoot().getParent()).endViewTransition(binding.getRoot());
((ViewGroup)binding.getRoot().getParent()).removeAllViews();
}
super.onDestroyView();
binding = null;
}
private void prepareSharedElementTransition() {
Transition transition = TransitionInflater.from(getContext()).inflateTransition(R.transition.shared_entity_image);
setSharedElementEnterTransition(transition);
setEnterSharedElementCallback(new SharedElementCallback() {
@Override
public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
Fragment currentFragment = (Fragment) binding.entityInfoViewPager.getAdapter().instantiateItem(binding.entityInfoViewPager, 0);
View view = currentFragment.getView();
if(view == null)
return;
sharedElements.put(names.get(0), view.findViewById(R.id.image));
}
});
}
private void onBackPressed() {
if(binding == null)
return;
if(binding.entityInfoTabLayout.getSelectedTabPosition() != 0) {
binding.entityInfoTabLayout.selectTab(binding.entityInfoTabLayout.getTabAt(0));
new Handler(Looper.getMainLooper()).postDelayed(() -> {
animateBottomNavigation();
requireActivity().onBackPressed();
}, 140);
}
else {
animateBottomNavigation();
requireActivity().onBackPressed();
}
}
private void animateBottomNavigation() {
BottomNavigationView bottomNavigationView = requireActivity().findViewById(R.id.main_bottom_navigation);
androidx.transition.Transition transition = new Fade(Fade.IN);
transition.setDuration(200);
transition.addTarget(bottomNavigationView);
TransitionManager.beginDelayedTransition(requireActivity().findViewById(R.id.activity_main_root_relative), transition);
bottomNavigationView.setVisibility(View.VISIBLE);
}
}
这是代码,但我在片段中没有得到任何关于异常的信息。