我正在尝试使用ViewPager + FragmentPagerAdapter.
我目前正在使用最新的 Android 支持包 v4切换片段
我的应用中有 5 个标签。当我选择第 4 个选项卡时,应用程序突然退出并显示“无法更改片段的标签 ~~”。
我没有尝试替换布局 xml 文件中包含的 Fragment,所以我无法找出原因。
这是发生错误的代码:
@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());
}
下面是我正在使用的 FragmentPagerAdapter 的片段
@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;
}
以下是我遇到的错误日志:
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)
提前致谢...