Unable to instantiate Fragment

后端 未结 12 2154
无人及你
无人及你 2020-12-24 06:42

Unable to instantiate fragment make sure class name exists, is public, and has an empty constructor that is public

Is it because my Fr

12条回答
  •  无人及你
    2020-12-24 07:32

    Using setRetainInstance(true) worked for us. Our inner classes now look like this:

    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.
            Fragment fragment = new MySectionFragment();
            Bundle args = new Bundle();
            args.putInt(MySectionFragment.ARG_SECTION_NUMBER, position + 1);
            fragment.setArguments(args);
            fragment.setRetainInstance(true);
            return fragment;
        }
        // ...
    }
    
    public class MySectionFragment extends Fragment {
        public static final String ARG_SECTION_NUMBER = "section_number";
        @SuppressLint("ValidFragment")
        public MySectionFragment() {
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            //...
        }       
        // ...
    }
    

    PS. Here's an interesting one about setRetainInstance(boolean): Understanding Fragment's setRetainInstance(boolean)

提交回复
热议问题