Android Fragment no view found for ID?

前端 未结 30 1965
逝去的感伤
逝去的感伤 2020-11-22 05:33

I have a fragment I am trying to add into a view.

FragmentManager fragMgr=getSupportFragmentManager();
feed_parser_activity content = (feed_parser_activity)f         


        
30条回答
  •  广开言路
    2020-11-22 06:08

    In my case I was trying to show a DialogFragment containing a pager and this exception was thrown when the FragmentPagerAdapter attempted to add the Fragments to the pager. Based on howettl answer I guess that it was due to the Pager parent was not the view set in setContentView() in my FragmentActivity.

    The only change I did to solve the problem was to create the FragmentPagerAdapter passing in a FragmentMager obtained by calling getChildFragmentManager(), not the one obtained by calling getFragmentManager() as I normally do.

        public class PagerDialog extends DialogFragment{
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
            View rootView = inflater.inflate(R.layout.pager_dialog, container, false);
    
            MyPagerAdapter pagerAdapter = new MyPagerAdapter(getChildFragmentManager());
            ViewPager pager = (ViewPager) rootView.findViewById(R.id.pager);
            pager.setAdapter(pagerAdapter);
    
            return rootView;
        }
    }
    

提交回复
热议问题