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

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

    1. As mentioned by @Justin Breitfeller, @Vidar Wahlberg solution is a hack which might not work in future version of Android.
    2. @Vidar Wahlberg perfer a hack because other solution might "cause the map to be recreated and redrawn, which isn't always desirable". Map redraw could be prevented by maintaining the old map fragment, rather than creating a new instance every time.
    3. @Matt solution doesn't work for me (IllegalStateException)
    4. As quoted by @Justin Breitfeller, "You cannot inflate a layout into a fragment when that layout includes a . Nested fragments are only supported when added to a fragment dynamically."

    My solution:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,                              Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_map_list, container, false);
    
        // init
        //mapFragment = (SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map);
        // don't recreate fragment everytime ensure last map location/state are maintain
        if (mapFragment == null) {
            mapFragment = SupportMapFragment.newInstance();
            mapFragment.getMapAsync(this);
        }
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        // R.id.map is a layout
        transaction.replace(R.id.map, mapFragment).commit();
    
        return view;
    }
    

提交回复
热议问题