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...
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With