5

I'm trying to switch Fragments using ViewPager + FragmentPagerAdapter. I'm currently using the latest Android support pack v4

I have 5 tabs in my app. When I select 4th tab, the app suddenly exits and says "can't change tag of fragment ~~".

I didn't try to replace the Fragment which is included in the layout xml file, so I couldn't figure out the cause.

Here is the code where the error occurs:

@Override
public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

Below is fragment of FragmentPagerAdapter I'm using

@Override
    public Fragment getItem(int i) {
        if (fragment_list.get(i) == null) {
            fragment_list.add(i, getFragment(i));
        }

        return fragment_list.get(i);
    }

    private Fragment getFragment(int index) {
        switch (index) {
        case TOP:
            return UseCaseListWithOptionFragment.newInstance(
                    URLHelper.USE_CASES_TOP_URL, R.array.comment);
        case FEED:
            int user_id = UserHelper.getCurrentUser(MainActivity.this).id;
            return UseCaseListFragment.newInstance(
                    URLHelper.getMyFeedsURL(user_id), true);
        case RECENT:
            return UseCaseListFragment.newInstance(
                    URLHelper.USE_CASES_RECENT_URL, true);
        case CATEOGORY:
            return UseCaseGroupListFragment.newInstance(
                    URLHelper.USE_CASE_GROUPS_URL, R.array.use_case);
        case MY:
            return UserProfileFragment.newInstance(UserHelper
                    .getCurrentUser(MainActivity.this));
        default:
            throw new IllegalStateException("Tab index out of bound.");
        }
    }

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

Below is the error log I encounter:

08-04 17:51:55.983: E/AndroidRuntime(1112): FATAL EXCEPTION: main
08-04 17:51:55.983: E/AndroidRuntime(1112): java.lang.IllegalStateException: Can't change tag of fragment UseCaseGroupListFragment{4055f558 id=0x7f050034 android:switcher:2131034164:3}: was android:switcher:2131034164:3 now android:switcher:2131034164:4
08-04 17:51:55.983: E/AndroidRuntime(1112):     at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:356)
08-04 17:51:55.983: E/AndroidRuntime(1112):     at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:347)
08-04 17:51:55.983: E/AndroidRuntime(1112):     at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:99)
08-04 17:51:55.983: E/AndroidRuntime(1112):     at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:692)
08-04 17:51:55.983: E/AndroidRuntime(1112):     at android.support.v4.view.ViewPager.populate(ViewPager.java:875)
08-04 17:51:55.983: E/AndroidRuntime(1112):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:469)
08-04 17:51:55.983: E/AndroidRuntime(1112):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:441)
08-04 17:51:55.983: E/AndroidRuntime(1112):     at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:422)
08-04 17:51:55.983: E/AndroidRuntime(1112):     at org.everyuse.android.activity.MainActivity.onTabSelected(MainActivity.java:176)
08-04 17:51:55.983: E/AndroidRuntime(1112):     at com.actionbarsherlock.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:526)
08-04 17:51:55.983: E/AndroidRuntime(1112):     at com.actionbarsherlock.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:907)
08-04 17:51:55.983: E/AndroidRuntime(1112):     at com.actionbarsherlock.internal.widget.ScrollingTabContainerView$TabClickListener.onClick(ScrollingTabContainerView.java:503)
08-04 17:51:55.983: E/AndroidRuntime(1112):     at android.view.View.performClick(View.java:2485)
08-04 17:51:55.983: E/AndroidRuntime(1112):     at android.view.View$PerformClick.run(View.java:9080)
08-04 17:51:55.983: E/AndroidRuntime(1112):     at android.os.Handler.handleCallback(Handler.java:587)
08-04 17:51:55.983: E/AndroidRuntime(1112):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-04 17:51:55.983: E/AndroidRuntime(1112):     at android.os.Looper.loop(Looper.java:123)
08-04 17:51:55.983: E/AndroidRuntime(1112):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-04 17:51:55.983: E/AndroidRuntime(1112):     at java.lang.reflect.Method.invokeNative(Native Method)
08-04 17:51:55.983: E/AndroidRuntime(1112):     at java.lang.reflect.Method.invoke(Method.java:507)
08-04 17:51:55.983: E/AndroidRuntime(1112):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-04 17:51:55.983: E/AndroidRuntime(1112):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-04 17:51:55.983: E/AndroidRuntime(1112):     at dalvik.system.NativeStart.main(Native Method)

Thanks in advance...

2
  • do u have an answer now?
    – singhsumit
    Dec 7, 2012 at 10:28
  • 1
    Your implementation of public Fragment getItem(int) seems a little strange. What would happen if getItem(4) was called before getItem(3)? I imagine you'd get an IndexOutOfBoundsException on the list. That's not happening here, but you might consider a slightly different approach and end up fixing the problem.
    – mkasberg
    Mar 24, 2013 at 5:20

2 Answers 2

4

I have experienced the same exception when incorrectly implementing the getCount method. Have you checked whether the correct size is returned?

@Override
public int getCount() {
    return 5;
}
1
  • I also had problems with this which turned out to be a mismatched public int getCount(), as bogdan suggested.
    – mkasberg
    Mar 24, 2013 at 5:19
1

I see that it's been a while you asked this question but for future user who might face this issue also...

I was having exact same error on some devices, and i couldn't find any reasone until i see @mkasberg's comment... Somehow getItem method may get called not in order, maybe because some devices process faster or i don't know, but using sparseArray instead of arrayList has resolved my problem. Thanks!

And here how you could do it:

public Fragment getItem(int i) {
    if (sparseArray.get(i) == null) {
        sparseArray.put(i, getFragment(i));
    }

    return sparseArray.get(i);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.