u010025993
shrimp-hamburger
采纳率81.1%
2013-08-08 06:32 阅读 2.6k

更新后出现CastException

更新框架:

@Override
public int getItemPosition(Object object) {
    FragmentPage2 f = (FragmentPage2) object;
    if (f != null) {
        f.update();
    }
    return super.getItemPosition(object);
}

现在如果添加一个额外的FragmentgetItem(int position)中,还一切正常,但是一运行mPager.getAdapter().notifyDataSetChanged();,就会报出异常:

08-07 19:45:50.844: E/AndroidRuntime(17776): java.lang.ClassCastException: com.foodbuilder.FragmentPage1 cannot be cast to com.foodbuilder.FragmentPage2
08-07 19:45:50.844: E/AndroidRuntime(17776):    at com.foodbuilder.MainActivity$MyAdapter.getItemPosition(MainActivity.java:967)
08-07 19:45:50.844: E/AndroidRuntime(17776):    at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:851)
08-07 19:45:50.844: E/AndroidRuntime(17776):    at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2772)
08-07 19:45:50.844: E/AndroidRuntime(17776):    at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
08-07 19:45:50.844: E/AndroidRuntime(17776):    at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
08-07 19:45:50.844: E/AndroidRuntime(17776):    at com.foodbuilder.MainActivity.onDialogPositiveClick(MainActivity.java:888)
08-07 19:45:50.844: E/AndroidRuntime(17776):    at com.foodbuilder.TablesDialogFragment$2.onClick(TablesDialogFragment.java:57)
08-07 19:45:50.844: E/AndroidRuntime(17776):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
` 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    Jave_ JaveZh 2013-08-08 06:52

    Fragment1不能强制转换为Fragment2
    你要么统一Fragament ,将所有Fragment加入一个list然后去get取出来

    点赞 评论 复制链接分享
  • lingling123www lingling123www 2013-08-09 15:37

    转换异常,只是把不同类的强行转是不行的。
    更好的方法:http://www.360cat.cn/it/note/bmwx12/info/0-289_0_0.html

    点赞 评论 复制链接分享

相关推荐