switching android map fragment -- crashed

447 Views Asked by At

In my application I have two tab (map and nearby). Both fragments are using map fragment.

Here is my map.xml

<RelativeLayout
            android:id="@+id/countryLayout"
            android:layout_below="@id/partnersLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/mainFooterLayout">

        <FrameLayout
                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"
                    android:name="com.google.android.gms.maps.MapFragment"/>

        </FrameLayout>

    </RelativeLayout>

Here is my inflated code

 View view = inflater.inflate(R.layout.map, container, false);

and in NearbyFragment and MyMapFragment I called

 public void onDestroyView() {
        super.onDestroyView();
        Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
        FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();
        ft.remove(fragment);
        ft.commit();
    }

but still my application crashed

I have this exception

Caused by: java.lang.IllegalArgumentException: Binary XML file line #42: Duplicate id 0x7f0600b5, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment

Could anyone tell me what is wrong here?

Is it possible that I use MapFragment instead of SupportMapFragment?

Thank you

1

There are 1 best solutions below

0
On

See what you should do:

1) Define activity layout:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

2) Create adapter for your tabulated pager:

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Context;
import android.support.v13.app.FragmentPagerAdapter;

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    List<Fragment> mValues = new ArrayList<Fragment>();

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);

        // HERE YOU ADD INSTANCES OF FRAGMENTS WHAT YOU NEED
        mValues.add(new com.google.android.gms.maps.MapFragment());
        mValues.add(new com.google.android.gms.maps.MapFragment());
    }

    @Override
    public Fragment getItem(int position) {
        return PlaceholderFragment.newInstance(position + 1);
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Map";
            case 1:
                return "Nearby";
        }
        return null;
    }
}

3) Create main activity with tabs

import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.view.ViewPager;

public class MainActivity extends Activity implements ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;

    ViewPager mViewPager;

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

        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });

        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(actionBar.newTab().setText(mSectionsPagerAdapter.getPageTitle(i)).setTabListener(this));
        }
    }

    @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) {
    }

}

Inside Adapter you have to create instances of fragmnets what you need to place in your activity like a tabs

P.S. sorry for my english and typo