我正在尝试使用ViewPager + FragmentPagerAdapter.
切换片段
我目前正在使用最新的Android支持包v4
我的应用中有5个标签。当我选择第四个标签时,应用程序突然退出并说“无法更改片段标记~~”。
我没有尝试替换布局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)
提前致谢...
答案 0 :(得分:4)
我在错误地实现getCount方法时遇到了同样的异常。您是否检查过是否返回了正确的尺寸?
@Override
public int getCount() {
return 5;
}
答案 1 :(得分:1)
我看到你问了这个问题已经有一段时间了但是对于未来可能面临这个问题的用户来说......
我在某些设备上遇到了完全相同的错误,在看到@ mkasberg的评论之前我找不到任何反应...不知怎的,getItem方法可能不按顺序调用,可能是因为某些设备处理速度更快或者我不喜欢知道,但使用sparseArray而不是arrayList解决了我的问题。 谢谢!
在这里你可以做到这一点:
public Fragment getItem(int i) {
if (sparseArray.get(i) == null) {
sparseArray.put(i, getFragment(i));
}
return sparseArray.get(i);
}