Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment already added IllegalStateException with only one copy of fragment

Tags:

android

I know there have been several questions about this exception, however, I'm experiencing this exception with only one assignment while using the FragmentStatePagerAdapter.

I indicated the line I'm experiencing the crash on below while setting up the adapter and pager.

Setting up my Fragments:

android.support.v4.app.FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.add(0, new StarTabFragment(), "starTab");
t.add(0, new HierarchiesTabFragment(), "hierarchyTab");
t.add(0, new GroupsTabFragment(), "groupTab");
t.add(0, new VehiclesTabFragment(), "hierarchyTab");
t.commit();

Setting up my adapter and pager:

viewPager = (ViewPager) mDrawerLayoutRight.findViewById(R.id.pager);
PagerTabStrip tabs = (PagerTabStrip) mDrawerLayoutRight.findViewById(R.id.pager_tab_strip);
tabs.setTabIndicatorColor(Color.RED);

CustomPagerAdapter customPagerAdapter = new CustomPagerAdapter(getSupportFragmentManager());
viewPager.setOffscreenPageLimit(customPagerAdapter.getCount() - 2);
viewPager.setAdapter(customPagerAdapter); //crashing here
viewPager.setOnPageChangeListener(new LoopingPageChangeListener(viewPager));
viewPager.setCurrentItem(1);

Stacktrace:

at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:876)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5343)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.IllegalStateException: Fragment already added: StarTabFragment{d12eb55 #1 id=0x7f0b0116 starTab}
    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1197)
    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:673)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
    at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
    at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
    at android.support.v4.view.ViewPager.setAdapter(ViewPager.java:447)
    at com.company.activities.MapsActivity.setupSliderRight(MapsActivity.java:847)
    at com.company.activities.MapsActivity.access$2100(MapsActivity.java:112)
    at com.company.activities.MapsActivity$DBHierarchyResponseReceiver.onReceive(MapsActivity.java:2287)
    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:866)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5343)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
like image 879
nukeforum Avatar asked Oct 17 '15 03:10

nukeforum


1 Answers

It seems sometimes that this is a generic error thrown when something occurred while loading the fragment.

For example: I had this error when inside onCreateView I called findViewById with a wrong id that didn't exist on fragment's layout.

So, make sure your fragment onCreateView's code doesn't has any bug (try commenting it all - only returning the raw inflated view - and see if it still throws this error.

like image 135
Isan Campos Avatar answered Oct 21 '22 16:10

Isan Campos