Android FragmentPagerAdapter与getChildFragmentManager()崩溃

Android FragmentPagerAdapter与getChildFragmentManager()崩溃,android,android-viewpager,Android,Android Viewpager,这是制作ViewPager的代码。错误是找不到视图,尽管视图存在 View attachmentBottomSheetView = LayoutInflater.from(container.getContext()).inflate(R.layout.attachment_view_pager_with_tabs, null); BottomSheetDialog attachmentBottomSheetDialog = new BottomSheetDialog(g

这是制作
ViewPager
的代码。错误是找不到视图,尽管视图存在

View attachmentBottomSheetView = LayoutInflater.from(container.getContext()).inflate(R.layout.attachment_view_pager_with_tabs, null);
            BottomSheetDialog attachmentBottomSheetDialog = new BottomSheetDialog(getActivity());
            attachmentBottomSheetDialog.setCanceledOnTouchOutside(true);
            attachmentBottomSheetDialog.setCancelable(true);
            DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
            ConstraintLayout.LayoutParams attachmentSheetParams = new ConstraintLayout.LayoutParams(
                    ConstraintLayout.LayoutParams.MATCH_PARENT,
                    displayMetrics.heightPixels);

        ViewPager viewPager = attachmentBottomSheetView.findViewById(R.id.viewPagerAttachment);
        final Bundle bundle = new Bundle();
        viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                AttachmentTabsFragment attachmentTabsFragment = new AttachmentTabsFragment();
                switch (position) {
                    case 0:
                        bundle.putString(KEY_ATTACHMENT_FRAGMENT_NAME, "Image");
                        break;
                    case 1:
                        bundle.putString(KEY_ATTACHMENT_FRAGMENT_NAME, "Video");
                        break;
                    case 2:
                        bundle.putString(KEY_ATTACHMENT_FRAGMENT_NAME, "GIF");
                        break;
                }
                attachmentTabsFragment.setArguments(bundle);
                return attachmentTabsFragment;
            }

            @Override
            public int getCount() {
                return 3;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                switch (position) {
                    case 0:
                        return "Image";
                    case 1:
                        return "Video";
                    case 2:
                        return "GIF";
                }
                return "";
            }
        });

        TabLayout tabLayout = attachmentBottomSheetView.findViewById(R.id.varun);
        tabLayout.setupWithViewPager(viewPager);

        window.setWindowAnimations(R.style.attachment_bottom_sheet_dialog_animation);
        attachmentBottomSheetDialog.setContentView(attachmentBottomSheetView, attachmentSheetParams);
        attachmentBottomSheetDialog.show();
错误是

java.lang.IllegalArgumentException:找不到片段附件选项卡{98eb499#7 id=0x7f0901b9 android:switcher:2131296697:0}的id 0x7f0901b9(com.example.circle:id/viewPagerAttachment)的视图


当我尝试用
getFragmentManager()
替换
getFragmentManager()
时,我得到了相同的错误

您的
ViewPager
存在于
BottomSheetDialog
中,它不是片段的一部分。而
BottomSheetDialog
是一个
对话框
而不是
片段


解决方案:-使用而不是
BottomSheetDialog
。然后,您可以访问底部工作表中
片段的所有功能

那么如何将视图设置为。。对于此行,attachmentBottomSheetDialog.setContentView(attachmentBottomSheetView,attachmentSheetParams)
DialogFragment
是DialogFragment
的子级。它有自己的设置视图的方法。只需按照教程进行操作。