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

前端 未结 23 1652
-上瘾入骨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:24

    I am a bit late to the party but Non of these answer helped me in my case. I was using Google map as SupportMapFragment and PlaceAutocompleteFragment both in my fragment. As all the answers pointed to the fact that the problem is with SupportMapFragment being the map to be recreated and redrawn.But after digging found out my problem was actually with PlaceAutocompleteFragment

    So here is the working solution for those who are facing this problem because of SupportMapFragment and SupportMapFragment

     //Global SupportMapFragment mapFragment;
     mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapFragment);
        FragmentManager fm = getChildFragmentManager();
    
        if (mapFragment == null) {
            mapFragment = SupportMapFragment.newInstance();
            fm.beginTransaction().replace(R.id.mapFragment, mapFragment).commit();
            fm.executePendingTransactions();
        }
    
        mapFragment.getMapAsync(this);
    
        //Global PlaceAutocompleteFragment autocompleteFragment;
    
    
        if (autocompleteFragment == null) {
            autocompleteFragment = (PlaceAutocompleteFragment) getActivity().getFragmentManager().findFragmentById(R.id.place_autoCompleteFragment);
    
        }
    

    And in onDestroyView clear the SupportMapFragment and SupportMapFragment

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    
    
        if (getActivity() != null) {
            Log.e("res","place dlted");
            android.app.FragmentManager fragmentManager = getActivity().getFragmentManager();
            android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.remove(autocompleteFragment);
            fragmentTransaction.commit(); 
           //Use commitAllowingStateLoss() if getting exception 
    
            autocompleteFragment = null;
        }
    
    
    }
    

提交回复
热议问题