将 viewpager 放入对话框错误“getSupportFragmentManager()”

Posted

技术标签:

【中文标题】将 viewpager 放入对话框错误“getSupportFragmentManager()”【英文标题】:Putting viewpager in a dialog error "getSupportFragmentManager()" 【发布时间】:2017-08-11 15:17:54 【问题描述】:

我正在尝试将 viewpager 放在自定义对话框中(wasabeef 的 blurdialogfragment),但我不知道问题出在哪里,也不知道如何替换它,因为它说:“无法解析方法 getSupportFragmentManager()”

如果有人能帮我解决这个问题,我将不胜感激..

代码:

 public class DialogHelperForNoobs extends BlurDialogFragment 

    String personName;
    String personGivenName;
    String personFamilyName;
    String personEmail;
    String personId;


    private SectionsPagerAdapter mSectionsPagerAdapter;

    private ViewPager mViewPager;

    public DialogHelperForNoobs() 
    

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) 
        return createDialogHelperForNoobs();
    

    public AlertDialog createDialogHelperForNoobs() 

        Bundle mArgs = getArguments();

        //Datos Usuario
        personName = mArgs.getString("personName");
        personGivenName = mArgs.getString("personGivenName");
        personFamilyName = mArgs.getString("personFamilyName");
        personEmail = mArgs.getString("personEmail");
        personId = mArgs.getString("personId");

        //INFLADOR DIALOGO
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View v = inflater.inflate(R.layout.activity_dialog_helper_for_noobs, null);

        //Error in FragmentManager getSupportFragmentManager()
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) v.findViewById(R.id.ViewPager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        builder.setView(v);




        return builder.create();
    


    public static class PlaceholderFragment extends Fragment 
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() 
        

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) 
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) 
            View rootView = inflater.inflate(R.layout.fragment_view_pager_noobs, container, false);
            TextView textView = (TextView) rootView.findViewById(R.id.section_label);
            textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
            return rootView;
        
    

    public class SectionsPagerAdapter extends FragmentPagerAdapter 

        public SectionsPagerAdapter(FragmentManager fm) 
            super(fm);
        

        @Override
        public Fragment getItem(int position) 
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(position + 1);
        

        @Override
        public int getCount() 
            // Show 3 total pages.
            return 3;
        

        @Override
        public CharSequence getPageTitle(int position) 
            switch (position) 
                case 0:
                    return "SECTION 1";
                case 1:
                    return "SECTION 2";
                case 2:
                    return "SECTION 3";
            
            return null;
        
    


有人可以帮忙吗?谢谢。

【问题讨论】:

***.com/a/27425568 @fluffyBatman 它告诉我 SectionsPagerAdapter 不能应用 getFragmentManager() SectionsPagerAdapter 的构造函数取什么? @fluffyBatman public SectionsPagerAdapter(FragmentManager fm) super(fm); 这是构造函数 【参考方案1】:

取决于您从哪里调用它,但我怀疑这是因为当 getSupportFragmentManagerAppCompatActivity 类上的方法时,您从片段中调用它

【讨论】:

我认为这就是问题所在,但是,我该如何解决呢?我改变什么方法?

以上是关于将 viewpager 放入对话框错误“getSupportFragmentManager()”的主要内容,如果未能解决你的问题,请参考以下文章

viewpager 中的片段,未找到视图错误

如何:将 TabbedLayout 放入 ViewPager

Android PullToRefreshListView和ViewPager的结合使用

对话框中的 Viewpager?

ViewPager的使用

ViewPager - 将数据从片段发送到活动时出现 Android 错误 setCurrentItem(int,boolean)