1

i have a basic navigation with 3 tabs. One of them contains a fragment with google maps in it. However the navigation isn't good, it keeps crashing after i select the tab for the second time.

It says it is because of the XML file of the maps which in my opinion doesn't have anything wrong. Can anyone see my error which i am blinded for right now?

Logcat :

android.view.InflateException: Binary XML file line #7: Error inflating class fragment
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
        at com.example.dell.animalz.MapsFragment.onCreateView(MapsFragment.java:22)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
        at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1297)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:672)
        at      

android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
        at   
android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:482)
        at     
android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
        at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:555)
        at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514)
        at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:495)
        at com.example.dell.animalz.HomeActivity.onTabSelected(HomeActivity.java:67)
        at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:674)
        at com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:1217)
        at     
com.android.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:976)
        at android.view.View.performClick(View.java:4637)
        at android.view.View$PerformClick.run(View.java:19422)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5586)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalArgumentException: Binary XML file line #7: Duplicate id 0x7f070067, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment
        at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)

            

HomeActivity.java

public class HomeActivity extends FragmentActivity implements ActionBar.TabListener {

ViewPager mViewPager = null;
private TabsMenuAdapter mAdapter;
private ActionBar actionBar;

private String []tabs = {"News", "Animalz", "Maps"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_layout);

    mViewPager = (ViewPager)findViewById(R.id.viewpager1);
    actionBar = getActionBar();
    mAdapter = new TabsMenuAdapter(getSupportFragmentManager());

    mViewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    for(String tab_names : tabs){
        actionBar.addTab(actionBar.newTab().setText(tab_names).setTabListener(this));
    }
    mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener(){
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    mViewPager.setCurrentItem(tab.getPosition());

}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {

}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    mViewPager.setCurrentItem(tab.getPosition());
}
}

Maps.java

public class MapsFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.maps_layout, container, false);
    return rootView;

  }
}

Maps.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>

Any help would be appreciated, thanks in advance !

zak bennani
  • 31
  • 1
  • 8
  • 1
    Note the cause of the exception: Binary XML file line #7: Duplicate id 0x7f070067, tag null, or parent id 0x0 with another fragment for com.google.android.gms.maps.SupportMapFragment – aioobe Dec 25 '14 at 10:29
  • why don't you add that fragment dynamically instead of by xml? – Mehul Joisar Dec 25 '14 at 10:41

2 Answers2

0

You have to remove the MapFragment in the onDestroyView() method of the Fragment that contains the map, with referring to this answer, you must do the following:

// override the onDestroyView of the fragment
@Override
public void onDestroyView() {
    super.onDestroyView();
    MapFragment f = (MapFragment) getFragmentManager()
                                         .findFragmentById(R.id.the_map);
    if (f != null) 
        getFragmentManager().beginTransaction().remove(f).commit();
}
Community
  • 1
  • 1
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
0

Every time check your parent view empty or not. If yes then remove. Change your Maps.java.

private static View view;    
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup group,Bundle bundle)
{
    if(view!=null)
    {
        ViewGroup parent =(ViewGroup)view.getParent();
        if(parent!=null)
            parent.removeView(view);
    }
    try{
        view=inflater.inflate(R.layout.maps, group, false);
        ........

    }
    catch(Exception e)
    {
        Log.e("ERROR", ""+e);
    }

    return view;
}
Jigar Shekh
  • 2,800
  • 6
  • 29
  • 54