0

I'm now following a viewpager guide in

http://developer.android.com/training/implementing-navigation/lateral.html

And I met a serious problem.

As I wrote in title, I tried to put the Google map v2 in each fragment of viewpager and I met this error. (Two ScreenSlidePageFragment gets generated at once and it inflates map for each them.)

Caused by: java.lang.IllegalArgumentException: Binary XML file line #8: Duplicate id 0x7f0a0059, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.MapFragment
        at android.app.Activity.onCreateView(Activity.java:4835)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
        at com.maurat.trackuz.ScreenSlidePageFragment.onCreateView(ScreenSlidePageFragment.java:39)

I think because this viewpager loads 2 pages at once at the first time and the map fragment's id gets duplicated.

ScreenSlidePagerAdapter

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {

    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Log.d(TAG, "getItem " + position);
        return new ScreenSlidePageFragment();
    }

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

ScreenSlidePageFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) {
    Log.d(TAG, "onCreateView");

    ViewGroup view = (ViewGroup) inflater.inflate(
            R.layout.fragment_screen_slide_page,
            container,
            false);

    return view;
}

fragment_screen_slide_page.xml

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

<fragment
    android:name="com.google.android.gms.maps.MapFragment"
    android:id="@+id/map_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</RelativeLayout>

So I've found and tried several solution but didn't found the solution for now. https://stackoverflow.com/a/14929151/1122517 https://stackoverflow.com/a/14695397/1122517

Now I'm finding a solution such like making a fragment programmatically.

Please help me.

Community
  • 1
  • 1
Hwangho Kim
  • 629
  • 4
  • 20

1 Answers1

1

Ok, I found a solution and it's maybe workaround but work perfectly.

What I wanted to make is a viewpager having map fragment.

First of all I made a basic of viewpager reference to below site

Using ViewPager for Screen Slides

and make a fragment class which to be a parent of 2 nested fragments

A fragment class for 2 nested fragments

public class TPageParentFragment extends Fragment {

private int position = -1;

/* Constructor */
public TPageParentFragment(int position) {
    this.position = position;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    ViewGroup rootView = (ViewGroup) inflater.inflate(
            R.layout.fragment_screen_slide_page, container, false);

    return rootView;
}

@Override
public void onResume() {
    Log.d(TAG, "onResume : " + this.position);
    super.onResume();

    // Here I add 2 nested fragments
    FragmentManager mFragmentManager = getChildFragmentManager();
    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();

    Fragment mapFragment = new TMapFragment();
    Fragment mapInfoFragment = new TMapInfoFragment();

    fragmentTransaction.add(R.id.fragContainer1, mapFragment);
    fragmentTransaction.add(R.id.fragContainer2, mapInfoFragment).commit();
}
}

And the xml of this parent fragment is

fragment_screen_slide_page.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#C7F464">

<FrameLayout
    android:id="@+id/fragContainer1"
    android:layout_width="fill_parent"
    android:layout_height="300dp"/>

<FrameLayout
    android:id="@+id/fragContainer2"
    android:layout_below="@+id/fragContainer1"
    android:layout_width="fill_parent"
    android:layout_height="500dp"/>

</RelativeLayout>

All of these FrameLayout will be changed with 2 nested fragments.

And 2 nested fragments are

TMapFragment

import com.google.android.gms.maps.SupportMapFragment;

public class TMapFragment extends SupportMapFragment {

public TMapFragment(){}

@Override
public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) {
    Log.d(TAG, "onCreateView");
    View v = super.onCreateView(arg0, arg1, arg2);
    initMap();
    return v;
}

private void initMap() {
    Log.d(TAG, "initMap");

}
}

and

TMapInfoFragment

public class TMapInfoFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Log.d(TAG, "onCreateView");

    ViewGroup rootView = (ViewGroup) inflater.inflate(
            R.layout.fragment_mapinfofragment, container, false);

    return rootView;
}
}

Surely you need to add a xml for fragment_mapinfofragment

Hwangho Kim
  • 629
  • 4
  • 20