几个月来,我一直在我的一个应用程序中使用 FragmentStatePagerAdapter,它运行良好。我最近切换到 IntelliJ IDEA 并更新到 ADT 22,现在当应用程序尝试破坏选项卡并创建新选项卡时,适配器会导致应用程序强制关闭。
正常工作时,Activity 将加载一系列选项卡。用户可以在它们之间滑动。所有这些仍然可以正常工作。
当用户在菜单中选择一个按钮时,应用程序应该销毁这些选项卡并创建新的选项卡。通常,这工作得很好,但现在它突然停止工作。这是代码:
/** TabsAdapter - Make Tabs Swipe-Able, Handle Fragments */
public static class TabsAdapter extends FragmentStatePagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
static final class TabInfo {
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
} // end TabInfo
public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = activity.getSupportActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
} // end TabsAdapter
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
notifyDataSetChanged();
} // end addTab
public void removeTab(ActionBar.Tab tab) {
mTabs.remove(tab.getTag());
mActionBar.removeTab(tab);
notifyDataSetChanged();
}
@Override
public int getItemPosition(Object object) {
return TabsAdapter.POSITION_NONE;
}
@Override
public int getCount() {
return mTabs.size();
} // end getCount()
@Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
} // end getItem()
@Override
public void onPageScrolled(int position, float positionOffest, int postionOffsetPixels){
} // end onPageScrolled
@Override
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
} // end onPageSelected
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Object tag = tab.getTag();
for (int i = 0; i < mTabs.size(); i++) {
if (mTabs.get(i) == tag) {
mViewPager.setCurrentItem(i);
}
}
} // end onTabSelected
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft){} // end onTabUnselected
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft){} // end onTabReselected
@Override
public void onPageScrollStateChanged(int arg0) {}
} // end TabsAdapter
在 onCreate() 期间,我调用了一个方法,我们称之为 CreateTabs1(),它成功构建了选项卡,用户可以滑动。只有当寻呼机试图破坏标签并创建新标签时,它才会突然失败,即使我再次调用 CreateTabs1() 也是如此。同样,这在以前总是完美无缺。
这是日志:
05-25 23:41:24.857: ERROR/AndroidRuntime(27290): FATAL EXCEPTION: main
java.lang.IllegalStateException: The application's PagerAdapter changed the adapter's contents without calling PagerAdapter#notifyDataSetChanged! Expected adapter item count: 11, found: 10 Pager id: com.######:id/pager Pager class: class android.support.v4.view.ViewPager Problematic adapter: class com.#####.######.#####$TabsAdapter
at android.support.v4.view.ViewPager.populate(ViewPager.java:959)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:548)
at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:507)
at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:488)
at com.#####.#####.#####$TabsAdapter.onTabSelected(#####.java:215)
at com.actionbarsherlock.internal.app.ActionBarWrapper$TabWrapper.onTabSelected(ActionBarWrapper.java:356)
at com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:570)
at com.android.internal.app.ActionBarImpl.removeTabAt(ActionBarImpl.java:544)
at com.android.internal.app.ActionBarImpl.removeTab(ActionBarImpl.java:520)
at com.actionbarsherlock.internal.app.ActionBarWrapper.removeTab(ActionBarWrapper.java:409)
at com.#####.#####.#####$TabsAdapter.removeTab(#####.java:181)
at com.#####.#####.#####.tabDrop(#####.java:381)
at com.#####.#####.#####.onOptionsItemSelected(#####.java:459)
at android.support.v4.app.Watson.onMenuItemSelected(Watson.java:118)
at com.actionbarsherlock.ActionBarSherlock.callbackOptionsItemSelected(ActionBarSherlock.java:603)
at com.actionbarsherlock.internal.ActionBarSherlockNative.dispatchOptionsItemSelected(ActionBarSherlockNative.java:93)
at com.actionbarsherlock.app.SherlockFragmentActivity.onMenuItemSelected(SherlockFragmentActivity.java:205)
at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
at com.android.internal.view.menu.SubMenuBuilder.dispatchMenuItemSelected(SubMenuBuilder.java:81)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:156)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
at android.widget.AbsListView$1.run(AbsListView.java:3423)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)