android - CastException: after Updating Fragment from ViewPager -


i'm updating fragment number 2 using:

@override public int getitemposition(object object) {     fragmentpage2 f = (fragmentpage2) object;     if (f != null) {         f.update();     }     return super.getitemposition(object); } 

now, when add , additional , different fragment in getitem(int position), seems working fine. however, when run mpager.getadapter().notifydatasetchanged(); throws me:

08-07 19:45:50.844: e/androidruntime(17776): java.lang.classcastexception: com.foodbuilder.fragmentpage1 cannot cast com.foodbuilder.fragmentpage2 08-07 19:45:50.844: e/androidruntime(17776):    @ com.foodbuilder.mainactivity$myadapter.getitemposition(mainactivity.java:967) 08-07 19:45:50.844: e/androidruntime(17776):    @ android.support.v4.view.viewpager.datasetchanged(viewpager.java:851) 08-07 19:45:50.844: e/androidruntime(17776):    @ android.support.v4.view.viewpager$pagerobserver.onchanged(viewpager.java:2772) 08-07 19:45:50.844: e/androidruntime(17776):    @ android.database.datasetobservable.notifychanged(datasetobservable.java:37) 08-07 19:45:50.844: e/androidruntime(17776):    @ android.support.v4.view.pageradapter.notifydatasetchanged(pageradapter.java:276) 08-07 19:45:50.844: e/androidruntime(17776):    @ com.foodbuilder.mainactivity.ondialogpositiveclick(mainactivity.java:888) 08-07 19:45:50.844: e/androidruntime(17776):    @ com.foodbuilder.tablesdialogfragment$2.onclick(tablesdialogfragment.java:57) 08-07 19:45:50.844: e/androidruntime(17776):    @ com.android.internal.app.alertcontroller$buttonhandler.handlemessage(alertcontroller.java:166) `  

what happening? thank much.

@override public int getitemposition(object object) {     if(object != null && object instanceof fragmentpage2){         fragmentpage2 f = (fragmentpage2) object;         f.update();             }     return super.getitemposition(object); } 

similarly can object class , check if assignable fragmentpage2.class:

if(object.getclass().isassignalbefrom(fragmentpage2.class))

this happens because have different classes inherit fragment , getitemposition() not care type of object, casting care type of object.

hope helps.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -