Kevin Suppan
Kevin Suppan

Reputation: 232

ViewPager with Fragments -> crash on second time selected

So I'm playing with the ViewPager and Fragments, but I'm stuck with a problem: I can swipe from the first to the third ViewPage/Fragment, but once I want to go back to the second or first, it just crashes.

Exception: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

My MainActivity (only a ViewPager in XML):

public void onCreate(Bundle savedInstanceState) {
        if(created)
            return;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());

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

        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));
        }
    }

public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int i) {
            Fragment fragment = null;
            switch (i) {
            case 0:
                fragment = new TrackingFragment();
                break;
            case 1:
                fragment = new TripFragment();
                break;
            case 2:
                fragment = new MapFragment();
                break;
            }

            Bundle args = new Bundle();
            args.putInt(TrackingFragment.ARG_SECTION_NUMBER, i + 1);
            fragment.setArguments(args);
            return fragment;
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase();
            case 1:
                return getString(R.string.title_section2).toUpperCase();
            case 2:
                return getString(R.string.title_section3).toUpperCase();
            }
            return null;
        }
    }

    public void onTabReselected(Tab tab, FragmentTransaction arg1) {
    }

    public void onTabSelected(Tab tab, FragmentTransaction arg1) {
        try {
            mViewPager.setCurrentItem(tab.getPosition());
        } catch (IllegalStateException e) {
        }
    }

And I have 3 fragments with one containing a LinearLayout, one as a ListFragment and one returning a MapView (using Android support libs).

Any ideas?

Upvotes: 2

Views: 2444

Answers (1)

JULI
JULI

Reputation: 126

This problem with Map View and fragment activity. I resolved it in my project with this

and this

Upvotes: 1

Related Questions