ViewPager DestroyItem无法转换对象

时间:2015-07-10 12:23:58

标签: c# android xamarin android-viewpager xamarin.android

我无法从ViewPager中的DestroyItem中删除视图 我按如下方式销毁视图:

public override void DestroyItem(ViewGroup container, int position, Java.Lang.Object @object)
{

    base.DestroyItem(container, position, @object);

    ((ViewPager)container).RemoveView((View)@object);

}


但发生InvalidCastException

07-10 07:57:04.002 I/MonoDroid( 8078): UNHANDLED EXCEPTION:
07-10 07:57:04.003 I/MonoDroid( 8078): System.InvalidCastException: Cannot cast from source type to destination type.
07-10 07:57:04.004 I/MonoDroid( 8078): at EAppPilot.Activities.ShoppingHistory.MyPagerAdapter.DestroyItem (Android.Views.ViewGroup,int,Java.Lang.Object) [0x00018] in d:\j.vso\bbalde\EApp2\EAppPilot\Activities\ShoppingHistory\ShoppingHistory2Activity.cs:824
07-10 07:57:04.004 I/MonoDroid( 8078): at Android.Support.V4.View.PagerAdapter.n_DestroyItem_Landroid_view_ViewGroup_ILjava_lang_Object_ (intptr,intptr,intptr,int,intptr) <IL 0x0001e, 0x000cd>
07-10 07:57:04.004 I/MonoDroid( 8078): at (wrapper dynamic-method) object.6c58d22f-eed1-48a2-a096-f94831f1ccdb (intptr,intptr,intptr,int,intptr) <IL 0x00023, 0x0003f>
07-10 07:57:04.008 W/art     ( 8078): JNI RegisterNativeMethods: attempt to register 0 native methods for md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable
07-10 07:57:04.010 E/InputEventReceiver( 8078): Exception dispatching input event.
07-10 07:57:04.010 E/MessageQueue-JNI( 8078): Exception in MessageQueue callback: handleReceiveCallback
07-10 07:57:04.011 E/MessageQueue-JNI( 8078): md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: System.InvalidCastException: Cannot cast from source type to destination type.
07-10 07:57:04.011 E/MessageQueue-JNI( 8078): at EAppPilot.Activities.ShoppingHistory.MyPagerAdapter.DestroyItem (Android.Views.ViewGroup,int,Java.Lang.Object) [0x00018] in d:\j.vso\bbalde\EApp2\EAppPilot\Activities\ShoppingHistory\ShoppingHistory2Activity.cs:824
07-10 07:57:04.011 E/MessageQueue-JNI( 8078): at Android.Support.V4.View.PagerAdapter.n_DestroyItem_Landroid_view_ViewGroup_ILjava_lang_Object_ (intptr,intptr,intptr,int,intptr) <IL 0x0001e, 0x000cd>
07-10 07:57:04.011 E/MessageQueue-JNI( 8078): at (wrapper dynamic-method) object.6c58d22f-eed1-48a2-a096-f94831f1ccdb (intptr,intptr,intptr,int,intptr) <IL 0x00023, 0x0003f>
07-10 07:57:04.011 E/MessageQueue-JNI( 8078): 
07-10 07:57:04.011 E/MessageQueue-JNI( 8078):   at md5ad5a12e23bdcfec4a812ffb2055e8203.MyPagerAdapter.n_destroyItem(Native Method)
07-10 07:57:04.011 E/MessageQueue-JNI( 8078):   at md5ad5a12e23bdcfec4a812ffb2055e8203.MyPagerAdapter.destroyItem(MyPagerAdapter.java:58)
07-10 07:57:04.011 E/MessageQueue-JNI( 8078):   at android.support.v4.view.ViewPager.populate(ViewPager.java:1039)
07-10 07:57:04.011 E/MessageQueue-JNI( 8078):   at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
07-10 07:57:04.011 E/MessageQueue-JNI( 8078):   at android.support.v4.view.ViewPager$3.run(ViewPager.java:250)
07-10 07:57:04.011 E/MessageQueue-JNI( 8078):   at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1847)
07-10 07:57:04.011 E/MessageQueue-JNI( 8078):   at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:1982)
07-10 07:57:04.011 E/MessageQueue-JNI( 8078):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2059)

我的片段只有axml

<android.support.v7.widget.RecyclerView 
 ~ />

和Page Main Activity axml

<android.support.v4.widget.DrawerLayout
  <LinearLayout
  <com.refractored.PagerSlidingTabStrip ~/>
    <FrameLayout
      <android.support.v4.view.ViewPager ~/>
    </FrameLayout>

</LinearLayout>
</android.support.v4.widget.DrawerLayout>

所以,我试试

((ViewPager)container).RemoveView((RecyclerView)@object);
((ViewPager)container).RemoveView((LinearLayout)@object);
((ViewPager)container).RemoveView((DrawerLayout)@object);
((ViewPager)container).RemoveView((FrameLayout)@object);
((ViewPager)container).RemoveView((View)@object);
((ViewPager)container).RemoveView((ViewPager)@object);

但所有相同的错误

1 个答案:

答案 0 :(得分:1)

public override void DestroyItem(ViewGroup container, int position, Object objectValue)
{
    base.DestroyItem(container, position, objectValue);

    var viewPager = container.JavaCast<ViewPager>();
    viewPager.RemoveView(objectValue as View);
}

使用JavaCast,RomeveView适合我