Duplicate ID, tag null, or parent id with another fragment for com.google.android.gms.maps.MapFragment

前端 未结 23 1593
-上瘾入骨i
-上瘾入骨i 2020-11-22 00:25

I have an application with three tabs.

Each tab has its own layout .xml file. The main.xml has its own map fragment. It\'s the one that shows up when the application

23条回答
  •  无人共我
    2020-11-22 01:23

    If you will use only Vidar Wahlberg answer, you get error when you open other activity (for example) and back to map. Or in my case open other activity and then from new activity open map again( without use back button). But when you combine Vidar Wahlberg solution and Matt solution you will have not exceptions.

    layout

    
    
        
    
            
        
    
    

    Fragment

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null){
                parent.removeView(view);
            }
        }
        try {
            view = inflater.inflate(R.layout.map_view, null);
            if(view!=null){
                ViewGroup root = (ViewGroup) view.findViewById(R.id.root);
    ...
    
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Fragment fragment = this.getSherlockActivity().getSupportFragmentManager().findFragmentById(R.id.map);
        if (fragment != null)
            getFragmentManager().beginTransaction().remove(fragment).commit();
    }
    

提交回复
热议问题