0

I used a tabhost for two fragments. Each fragment contains a google map.The question is it performs well in each fragment, but when I switch back the same fragment it crushed.
This is the error messages:

05-10 14:40:02.438  19480-19480/com.nthu.test.nthuapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #6: Error inflating class fragment
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
        at com.nthu.test.nthuapp.MapGuiding$TabChild.onCreateView(MapGuiding.java:263)
        ...
 Caused by: java.lang.IllegalArgumentException: Binary XML file line #6: Duplicate id 0x7f09005b, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
        at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2164)
        ...

I've found many solutions on web. Every one told that I have to remove fragment in onDestroy(), but it still not work on me.

This is my fragment content:

 View v;

  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }            
        if(v!=null){
            ViewGroup parent = (ViewGroup) v.getParent();
            if (parent != null)
                parent.removeView(v);
        }
        /*------------------this is line 263 that caused error----------*/
        v = inflater.inflate(R.layout.allfragment, container, false);
        Bundle args = getArguments();
        if (args != null && args.containsKey(ARGUMENT_NAME)) {
            String text = args.getString(ARGUMENT_NAME);
            if (mMap == null) {
                mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.allmap)).getMap();
            }
            if (mMap != null) {
                if(text.equals("all")){
                    setUpAllMap();
                }
                else if(text.equals("art")){
                    setUpArtMap();
                }
            }



@Override
    public void onDestroyView()
    {
        super.onDestroyView();
        killOldMap();
        mMap = null;
    }
    private void killOldMap() {
        SupportMapFragment mapFragment = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.allmap));
        if(mapFragment != null) {
            FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
            ft.remove(mapFragment);
            ft.commit();
        }
    }

I've tried many solutions on web but the problem still remains.Could anyone help me with this problem?
Thanks a lot!

Bonnie
  • 1
  • 1

1 Answers1

0

Have you tried adding the following code to your map fragment? Maybe you have omit something? If you haven't tried, please give it a shot.

public void onDestroyView() {
    super.onDestroyView();
    FragmentManager fm = getActivity().getSupportFragmentManager();
    Fragment fragment = (fm.findFragmentById(R.id.map));
    FragmentTransaction ft = fm.beginTransaction();
    ft.remove(fragment);
    ft.commit();
}
so_jin_ee
  • 802
  • 6
  • 7