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

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

    In this solution you do not need to take static variable;

    Button nextBtn;
    
    private SupportMapFragment mMapFragment;
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
    
        if (mRootView != null) {
            ViewGroup parent = (ViewGroup) mRootView.getParent();
            Utility.log(0,"removeView","mRootView not NULL");
            if (parent != null) {
                Utility.log(0, "removeView", "view removeViewed");
                parent.removeAllViews();
            }
        }
        else {
            try {
                mRootView = inflater.inflate(R.layout.dummy_fragment_layout_one, container, false);//
            } catch (InflateException e) {
        /* map is already there, just return view as it is  */
                e.printStackTrace();
            }
        }
    
        return  mRootView;
    }
    
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        FragmentManager fm = getChildFragmentManager();
        SupportMapFragment mapFragment = (SupportMapFragment) fm.findFragmentById(R.id.mapView);
        if (mapFragment == null) {
            mapFragment = new SupportMapFragment();
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(R.id.mapView, mapFragment, "mapFragment");
            ft.commit();
            fm.executePendingTransactions();
        }
        //mapFragment.getMapAsync(this);
        nextBtn = (Button) view.findViewById(R.id.nextBtn);
        nextBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utility.replaceSupportFragment(getActivity(),R.id.dummyFragment,dummyFragment_2.class.getSimpleName(),null,new dummyFragment_2());
            }
        });
    
    }`
    

提交回复
热议问题