我的应用程序有一个 SlidingMenu,菜单中的每个项目都是一个新的 SherlockFragment。大多数都是简单的单窗格片段,但我遇到了一个应该是两窗格的问题。我决定使用带有 ActionBar 选项卡的 ViewPager 来实现它。
ViewPager 有两个页面,CustomerFragment(客户列表)和 CartFragment(购物车项目网格,顶部有一个 TextView 以显示所选客户的名称)。当用户单击某人的姓名时,ViewPager 会翻页并显示姓名。由于它都包含在父片段中,我决定实现 ViewPager 的最佳方法是使用 ChildFragmentManager:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mViewPager = new ViewPager(activity);
mViewPager.setId(1);
v = mViewPager;
fm = getChildFragmentManager();
custFrag = CashRegisterCustomerFragment.newInstance();
cartFrag = CashRegisterCartFragment.newInstance();
fm.beginTransaction().add(mViewPager.getId(), custFrag).commitAllowingStateLoss();
fm.beginTransaction().add(mViewPager.getId(), cartFrag).commitAllowingStateLoss();
mActionBar = activity.getSupportActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mTabsAdapter = new CashRegisterTabsAdapter(activity, mViewPager);
mTabsAdapter.addTab(mActionBar.newTab().setText(activity.getResources().getString(R.string.customer_list)),
custFrag);
mTabsAdapter.addTab(mActionBar.newTab().setText(activity.getResources().getString(R.string.items)),
cartFrag);
if (savedInstanceState != null) {
mActionBar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
} else {
mActionBar.setSelectedNavigationItem(0);
}
return v;
}
CashRegisterTabsAdapter 的重要部分:
public CashRegisterTabsAdapter(MyActivity act, ViewPager pager) {
super(act.getSupportFragmentManager());
activity = act;
mActionBar = activity.getSupportActionBar();
mActionBar.setTitle(R.string.cash_register);
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, BaseFragment frag) {
tab.setTabListener(this);
mTabs.add(frag);
mActionBar.addTab(tab);
notifyDataSetChanged();
}
@Override
public int getCount() {
return mTabs.size();
}
@Override
public Fragment getItem(int position) {
return mTabs.get(position);
}
@Override
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
if (position == 1)
activity.getSlidingMenu().setSlidingEnabled(false);
else
activity.getSlidingMenu().setSlidingEnabled(true);
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
int pos = tab.getPosition();
if (mViewPager != null) {
mViewPager.setCurrentItem(pos);
}
}
一切都很好。但是,当我通过 SlidingMenu 切换片段然后点击主页按钮,将应用程序发送到后台时,我遇到了这个:
07-02 18:04:31.170: E/AndroidRuntime(32619): FATAL EXCEPTION: main
07-02 18:04:31.170: E/AndroidRuntime(32619): java.lang.IllegalStateException: Failure saving state: active CashRegisterCustomerFragment{41a8f1a0} has cleared index: -1
07-02 18:04:31.170: E/AndroidRuntime(32619): at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1700)
07-02 18:04:31.170: E/AndroidRuntime(32619): at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:527)
07-02 18:04:31.170: E/AndroidRuntime(32619): at com.actionbarsherlock.app.SherlockFragmentActivity.onSaveInstanceState(SherlockFragmentActivity.java:127)
07-02 18:04:31.170: E/AndroidRuntime(32619): at com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity.onSaveInstanceState(SlidingFragmentActivity.java:50)
07-02 18:04:31.170: E/AndroidRuntime(32619): at com.mycom.myapp.MyActivity.onSaveInstanceState(MyActivity.java:128)
07-02 18:04:31.170: E/AndroidRuntime(32619): at com.mycom.myapp.MainFragmentContainer.onSaveInstanceState(MainFragmentContainer.java:124)
07-02 18:04:31.170: E/AndroidRuntime(32619): at android.app.Activity.performSaveInstanceState(Activity.java:1137)
07-02 18:04:31.170: E/AndroidRuntime(32619): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1215)
07-02 18:04:31.170: E/AndroidRuntime(32619): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3077)
07-02 18:04:31.170: E/AndroidRuntime(32619): at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3136)
07-02 18:04:31.170: E/AndroidRuntime(32619): at android.app.ActivityThread.access$900(ActivityThread.java:142)
07-02 18:04:31.170: E/AndroidRuntime(32619): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1235)
07-02 18:04:31.170: E/AndroidRuntime(32619): at android.os.Handler.dispatchMessage(Handler.java:99)
07-02 18:04:31.170: E/AndroidRuntime(32619): at android.os.Looper.loop(Looper.java:137)
07-02 18:04:31.170: E/AndroidRuntime(32619): at android.app.ActivityThread.main(ActivityThread.java:4931)
07-02 18:04:31.170: E/AndroidRuntime(32619): at java.lang.reflect.Method.invokeNative(Native Method)
07-02 18:04:31.170: E/AndroidRuntime(32619): at java.lang.reflect.Method.invoke(Method.java:511)
07-02 18:04:31.170: E/AndroidRuntime(32619): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
07-02 18:04:31.170: E/AndroidRuntime(32619): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
07-02 18:04:31.170: E/AndroidRuntime(32619): at dalvik.system.NativeStart.main(Native Method)
奇怪的是,当我按下 CashRegister 父片段上的主页按钮时它不会崩溃,它只会在我第一次转到不同的父片段时抛出这个异常。
该站点说 FragmentManager 正在尝试在不允许的情况下进行事务,但我找不到可能的位置。当我在 onCreateView() 中删除 fm.beginTransaction.add() 调用时,它不会给出此错误,但是我无法在片段之间进行通信,因为在接口调用中没有要引用的父片段。