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

前端 未结 23 1927
-上瘾入骨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条回答
  •  Happy的楠姐
    2020-11-22 01:28

    This is my answer:

    1, Create a layout xml like following:

    
    
    

    2, in the Fragment class, add a google map programmatically.

    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.SupportMapFragment;
    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentTransaction;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    
    /**
     * A simple {@link android.support.v4.app.Fragment} subclass. Activities that
     * contain this fragment must implement the
     * {@link MapFragment.OnFragmentInteractionListener} interface to handle
     * interaction events. Use the {@link MapFragment#newInstance} factory method to
     * create an instance of this fragment.
     * 
     */
    public class MapFragment extends Fragment {
        // TODO: Rename parameter arguments, choose names that match
        private GoogleMap mMap;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_map, container, false);
            SupportMapFragment mMapFragment = SupportMapFragment.newInstance();
            mMap = mMapFragment.getMap();
            FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
            transaction.add(R.id.map_container, mMapFragment).commit();
            return view;
        }
    
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            Log.d("Attach", "on attach");
        }
    
        @Override
        public void onDetach() {
            super.onDetach();
        }
    } 
    

提交回复
热议问题