Android 无法将ViewPager中的片段替换为PopupWindow

Android 无法将ViewPager中的片段替换为PopupWindow,android,android-viewpager,fragment,popupwindow,Android,Android Viewpager,Fragment,Popupwindow,我正在尝试将viewpager添加到popupwindow中,但我无法做到这一点,我总是得到一个错误,即java.lang.IllegalArgumentException:fragment FirstFragment{875be04#0 id=0x7f0902d9 android:switcher:2131296985:0}的id 0x7f0902d9(:id/viewpager)找不到视图 我尝试了各种可能性,但没有任何结果 这是我的密码 public void showEditingPop

我正在尝试将viewpager添加到popupwindow中,但我无法做到这一点,我总是得到一个错误,即java.lang.IllegalArgumentException:fragment FirstFragment{875be04#0 id=0x7f0902d9 android:switcher:2131296985:0}的id 0x7f0902d9(:id/viewpager)找不到视图

我尝试了各种可能性,但没有任何结果

这是我的密码

public void showEditingPopupWindow() {

        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        if (inflater != null) {
            View view = inflater.inflate(R.layout.editor_layout, new LinearLayout(this));

            popupWindow = new PopupWindow();
            popupWindow.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            popupWindow.setOutsideTouchable(false);
            popupWindow.setContentView(view);

            popupWindow.setFocusable(true);
            popupWindow.getContentView().requestFocus();

            LinearLayout llSelectedColor, llBackgroundSelectedTextColor;

            ViewPager viewPager = view.findViewById(R.id.viewpager);
            setupViewPager(viewPager);
            TabLayout tabLayout = view.findViewById(R.id.tabs);
            tabLayout.setupWithViewPager(viewPager);
            tabLayout.setTabMode(TabLayout.MODE_FIXED);
            tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

            View headerView = inflater.inflate(R.layout.custom_tab, new LinearLayout(this));
            LinearLayout llTextStyle = headerView.findViewById(R.id.llTextStyle);
            LinearLayout llBackground = headerView.findViewById(R.id.llBackground);
            LinearLayout llTextColor = headerView.findViewById(R.id.llTextColor);
            llSelectedColor = headerView.findViewById(R.id.llSelectedColor);
            llBackgroundSelectedTextColor = headerView.findViewById(R.id.llBackgroundSelectedTextColor);

            tabLayout.getTabAt(0).setCustomView(llTextStyle);
            tabLayout.getTabAt(1).setCustomView(llBackground);
            tabLayout.getTabAt(2).setCustomView(llTextColor);

            llSelectedColor.setBackgroundColor(Color.parseColor(selectedBackgroundColor));
            llBackgroundSelectedTextColor.setBackgroundColor(Color.parseColor(selectedTextColor));

            DocumentColorChangeListener documentColorChangeListener = (color, isText) -> {
                if (isText) {
                    if (!selectedTextColor.equals(color)) {
                        selectedTextColor = color;
                        llBackgroundSelectedTextColor.post(() -> llBackgroundSelectedTextColor.setBackgroundColor(Color.parseColor(color)));
                    }
                } else if (!selectedBackgroundColor.equals(color)) {
                    selectedBackgroundColor = color;
                    llSelectedColor.setBackgroundColor(Color.parseColor(color));
                }
            };
            setOnColorUpdateListener(documentColorChangeListener);

            llTextEditor.setSelected(true);

            popupWindow.setOnDismissListener(() -> {
                llTextEditor.setSelected(false);
            });

            view.measure(Utils.makeViewMeasureSpec(popupWindow.getWidth()), Utils.makeViewMeasureSpec(popupWindow.getHeight()));
            llTextEditor.measure(0, 0);
            popupWindow.showAsDropDown(llTextEditor, -(popupWindow.getContentView().getMeasuredWidth() / 2) + llLanguageSelect.getMeasuredWidth() / 2, 0);
        }
    }
使用片段设置视图寻呼机

    private void setupViewPager(ViewPager viewPager) {

        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());

        Bundle bundle = new Bundle();
        bundle.putString(AppConstants.OtherConstants.SELECTED_TEXT_STYLE, selectedTextStyle);
        TextStyleFragment textStyleFragment = new TextStyleFragment();
        textStyleFragment.setArguments(bundle);

        bundle = new Bundle();
        BackgroundFragment backgroundFragment = new BackgroundFragment();
        bundle.putString(AppConstants.OtherConstants.SELECTED_BACKGROUND_COLOR, selectedBackgroundColor);
        backgroundFragment.setArguments(bundle);

        bundle = new Bundle();
        TextColorPickerFragment textColorPickerFragment = new TextColorPickerFragment();
        bundle.putString(AppConstants.OtherConstants.SELECTED_TEXT_COLOR, selectedTextColor);
        textColorPickerFragment.setArguments(bundle);


        viewPagerAdapter.addFragment(textStyleFragment, AppConstants.OtherConstants.TEXT_STYLE);
        viewPagerAdapter.addFragment(backgroundFragment, AppConstants.OtherConstants.BACKGROUND);
        viewPagerAdapter.addFragment(textColorPickerFragment, AppConstants.OtherConstants.COLOR_PICKER);
        viewPager.setAdapter(viewPagerAdapter);
        viewPager.setOffscreenPageLimit(3);
    }

这是我的viewpager适配器

    public class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        ViewPagerAdapter(android.support.v4.app.FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }


请帮助解决此问题

我使用DialogFragment解决了我的问题,我在其中替换了带有Viewpager和tabs的Fragment。请参阅下面的代码

public class DocumentEditingDialog extends BaseDialogFragment {

    public DocumentEditingDialog() {
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        setRetainInstance(false);
        ButterKnife.bind(Objects.requireNonNull(getActivity()));
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

        Window window = getDialog().getWindow();
        if (window != null) {
            window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); // This flag is required to set otherwise the setDimAmount method will not show any effect
            window.setDimAmount(0); //0 for no dim to 1 for full dim

            WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
            wmlp.gravity = Gravity.TOP;

            Bundle arguments = getArguments();
            if (arguments != null) {
                if (Utils.isTabletDevice(getActivity())) {
                    if (getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                        wmlp.x = arguments.getInt("x") - dpToPx(getResources().getDimension(R.dimen._105sdp));
                        wmlp.y = arguments.getInt("y") - dpToPx(getResources().getDimension(R.dimen._115sdp));
                    } else {
                        wmlp.x = arguments.getInt("x") - dpToPx(getResources().getDimension(R.dimen._175sdp));
                        wmlp.y = arguments.getInt("y") - dpToPx(getResources().getDimension(R.dimen._50sdp));
                    }
                } else {
                    wmlp.x = arguments.getInt("x") - dpToPx(getResources().getDimension(R.dimen._75sdp));
                    wmlp.y = arguments.getInt("y") - dpToPx(getResources().getDimension(R.dimen._72sdp));
                }
            }
            window.setAttributes(wmlp);
        }

        View view = inflater.inflate(R.layout.popup_windows_doc_editing, container, false);

        android.support.v4.app.FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        Bundle bundle = new Bundle();
        TextEditorFragment myf = new TextEditorFragment();
        myf.setArguments(bundle);
        transaction.replace(R.id.frm_language_list, myf);
        transaction.commit();
        return view;
    }

    public int dpToPx(float valueInDp) {
        DisplayMetrics metrics = Objects.requireNonNull(getActivity()).getResources().getDisplayMetrics();
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, metrics);
    }
}

我用DialogFragment解决了我的问题,在其中我替换了带有Viewpager和tabs的Fragment。请参阅下面的代码

public class DocumentEditingDialog extends BaseDialogFragment {

    public DocumentEditingDialog() {
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        setRetainInstance(false);
        ButterKnife.bind(Objects.requireNonNull(getActivity()));
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

        Window window = getDialog().getWindow();
        if (window != null) {
            window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); // This flag is required to set otherwise the setDimAmount method will not show any effect
            window.setDimAmount(0); //0 for no dim to 1 for full dim

            WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes();
            wmlp.gravity = Gravity.TOP;

            Bundle arguments = getArguments();
            if (arguments != null) {
                if (Utils.isTabletDevice(getActivity())) {
                    if (getActivity().getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                        wmlp.x = arguments.getInt("x") - dpToPx(getResources().getDimension(R.dimen._105sdp));
                        wmlp.y = arguments.getInt("y") - dpToPx(getResources().getDimension(R.dimen._115sdp));
                    } else {
                        wmlp.x = arguments.getInt("x") - dpToPx(getResources().getDimension(R.dimen._175sdp));
                        wmlp.y = arguments.getInt("y") - dpToPx(getResources().getDimension(R.dimen._50sdp));
                    }
                } else {
                    wmlp.x = arguments.getInt("x") - dpToPx(getResources().getDimension(R.dimen._75sdp));
                    wmlp.y = arguments.getInt("y") - dpToPx(getResources().getDimension(R.dimen._72sdp));
                }
            }
            window.setAttributes(wmlp);
        }

        View view = inflater.inflate(R.layout.popup_windows_doc_editing, container, false);

        android.support.v4.app.FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        Bundle bundle = new Bundle();
        TextEditorFragment myf = new TextEditorFragment();
        myf.setArguments(bundle);
        transaction.replace(R.id.frm_language_list, myf);
        transaction.commit();
        return view;
    }

    public int dpToPx(float valueInDp) {
        DisplayMetrics metrics = Objects.requireNonNull(getActivity()).getResources().getDisplayMetrics();
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, metrics);
    }
}

我真的不认为您能够在
弹出窗口中使用
ViewPager
Fragment
s;至少不需要一些真正的重担。
碎片管理器
正在
活动
的层次结构中查找
查看页面
,这就是为什么会出现
异常
,因为
弹出窗口
未附加到
活动
。如果您真的需要在
查看页面
中使用
片段
,则可以将其粘贴在
对话框Fragment
中,并使用其中的子
片段管理器
。如果出于任何原因,您必须使用
PopupWindow
,则在
ViewPager
中使用普通的
View
s。发送回复,您能告诉我如何使用与
PopupWindow
相同的
DialogFragment
来显示特定位置吗?这有点棘手。如果您只需将
ViewPager
更改为使用普通
View
s,可能会更容易。但是,如果你真的想坚持使用
片段
s,你首先要用
视图#getLocationOnScreen()
获得锚定
视图
的坐标。然后,在
对话框片段
中,从
WindowManager.LayoutParams wmlp…
行开始,在
对话框
的窗口上设置图形坐标,如中所示。您可以在构建自己的
对话框之后,或者在
onStart()
中,使用从
getDialog()返回的
对话框
,使用
查看页面
,在
弹出窗口中使用
片段
;至少不需要一些真正的重担。
碎片管理器
正在
活动
的层次结构中查找
查看页面
,这就是为什么会出现
异常
,因为
弹出窗口
未附加到
活动
。如果您真的需要在
查看页面
中使用
片段
,则可以将其粘贴在
对话框Fragment
中,并使用其中的子
片段管理器
。如果出于任何原因,您必须使用
PopupWindow
,则在
ViewPager
中使用普通的
View
s。发送回复,您能告诉我如何使用与
PopupWindow
相同的
DialogFragment
来显示特定位置吗?这有点棘手。如果您只需将
ViewPager
更改为使用普通
View
s,可能会更容易。但是,如果你真的想坚持使用
片段
s,你首先要用
视图#getLocationOnScreen()
获得锚定
视图
的坐标。然后,在
对话框片段
中,从
WindowManager.LayoutParams wmlp…
行开始,在
对话框
的窗口上设置图形坐标,如中所示。您可以在构建自己的
对话框之后,或者在
onStart()
中,使用从
getDialog()返回的
对话框
执行此操作