我試圖切換使用ViewPager + FragmentPagerAdapter.
目前我使用的是最新的Android支持包V4:使用ViewPager#setCurrentItem()時
我在我的應用程序中有5個選項卡。當我選擇第4個標籤時,應用程序突然退出,並說「無法更改片段標籤~~」。
我沒有嘗試替換佈局xml文件中包含的片段,所以我找不出原因。
這裏是哪裏發生錯誤的代碼:
@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)
在此先感謝...
做ü現在有答案嗎? – singhsumit 2012-12-07 10:28:41
你的'public Fragment getItem(int)'的實現看起來有點奇怪。 getItem(4)'在'getItem(3)'之前被調用會發生什麼?我想你會在列表中得到一個IndexOutOfBoundsException。這不是在這裏發生的,但你可能會考慮一種稍微不同的方法,並最終解決問題。 – mkasberg 2013-03-24 05:20:40