duplicated id with fragment

后端 未结 3 967
情歌与酒
情歌与酒 2020-12-01 07:00

I\'m trying to applicate drawernavigation (my first fragment is a map & the others are just some fragments with simple layouts).So it runs fine & I can navigate betw

3条回答
  •  温柔的废话
    2020-12-01 07:39

    Try to reuse/recycle your layout. I am running into "duplicate id" when using a map fragment. So in onCreateView instead of

    final View rootView = inflater.inflate(R.layout.fragment_profile, container, false);
    

    i am using

    public class YourFragment extends Fragment {
       public YourFragment(){}
       ...
       private static View rootView;
       ...
    
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        //View rootView = inflater.inflate(R.layout.fragment_layout, container, false);
    
        if (rootView != null) {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null)
                parent.removeView(rootView);
        }
        try {
            rootView = inflater.inflate(R.layout.fragment_layout, container, false);
        } catch (InflateException e) {
            /* map is already there, just return view as it is  */
        }
    

提交回复
热议问题