2 u010025993 u010025993 于 2013.08.08 14:32 提问

更新后出现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_
Jave_   2013.08.08 14:52
已采纳

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

lingling123www
lingling123www   2013.08.09 23:37

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

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!