Xamarin.forms Android ListView - ' jobject'不能是IntPtr.Zero

时间:2014-09-15 11:59:26

标签: android listview xamarin

所以我在我的app中使用了这个ListView,它实例化了一次。 ListItem源是一个List,其中我是一个TemplateSelector,它根据列表中存储的对象类型显示不同的单元格。

如果我

我的问题就出现了
  1. 在列表中选择一行
  2. 弹出上一页
  3. 按我的列表
  4. 推送到我的页面
  5. 选择列表中的项目
  6. 崩溃。
  7. 我收到以下错误,

    [MonoDroid] UNHANDLED EXCEPTION:
    [MonoDroid] System.ArgumentException: 'jobject' must not be IntPtr.Zero.
    [MonoDroid] Parameter name: jobject
    [MonoDroid] at Android.Runtime.JNIEnv.CallIntMethod (intptr,intptr) [0x00010] in /Users/builder/data/lanes/1131/2a7b6821/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:343
    [MonoDroid] at Android.Widget.AdapterView.get_FirstVisiblePosition () [0x00043] in /Users/builder/data/lanes/1131/2a7b6821/source/monodroid/src/Mono.Android/platforms/android-16/src/generated/Android.Widget.AdapterView.cs:866
    [MonoDroid] at Xamarin.Forms.Platform.Android.ListViewAdapter.SelectItem (object) <IL 0x0001f, 0x00080>
    [MonoDroid] at Xamarin.Forms.Platform.Android.ListViewAdapter.OnItemSelected (object,Xamarin.Forms.SelectedItemChangedEventArgs) <IL 0x00017, 0x00077>
    [MonoDroid] at (wrapper delegate-invoke) System.EventHandler`1<Xamarin.Forms.SelectedItemChangedEventArgs>.invoke_void_object_TEventArgs (object,Xamarin.Forms.SelectedItemChangedEventArgs) <0x0007b>
    [MonoDroid] at (wrapper delegate-invoke) System.EventHandler`1<Xamarin.Forms.SelectedItemChangedEventArgs>.invoke_void_object_TEventArgs (object,Xamarin.Forms.SelectedItemChangedEventArgs) <0x0003f>
    [MonoDroid] at Xamarin.Forms.ListView.OnSelectedItemChanged (Xamarin.Forms.BindableObject,object,object) <IL 0x0001c, 0x000b7>
    [MonoDroid] at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty,object,bool,bool,bool) <IL 0x000b9, 0x0032e>
    [MonoDroid] at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty,object,bool,bool,bool) <IL 0x00213, 0x007b7>
    [MonoDroid] at Xamarin.Forms.ListView.NotifyRowTapped (int,int) <IL 0x0004b, 0x0015b>
    [MonoDroid] at Xamarin.Forms.ListView.NotifyRowTapped (int) <IL 0x0001c, 0x0009f>
    [MonoDroid] at Xamarin.Forms.Platform.Android.ListViewAdapter.OnRealItemClicked (object,Android.Widget.AdapterView/ItemClickEventArgs) <IL 0x00025, 0x000bb>
    [MonoDroid] at Android.Widget.AdapterView/IOnItemClickListenerImplementor.OnItemClick (Android.Widget.AdapterView,Android.Views.View,int,long) [0x0000d] in /Users/builder/data/lanes/1131/2a7b6821/source/monodroid/src/Mono.Android/platforms/android-16/src/generated/Android.Widget.AdapterView.cs:261
    [MonoDroid] at Android.Widget.AdapterView/IOnItemClickListenerInvoker.n_OnItemClick_Landroid_widget_AdapterView_Landroid_view_View_IJ (intptr,intptr,intptr,intptr,int,long) [0x00019] in /Users/builder/data/lanes/1131/2a7b6821/source/monodroid/src/Mono.Android/platforms/android-16/src/generated/Android.Widget.AdapterView.cs:194
    [MonoDroid] at (wrapper dynamic-method) object.09b491e2-61b2-4fd3-b387-65f476ed0dfb (intptr,intptr,intptr,intptr,int,long) <IL 0x00029, 0x00047>
    

    有什么建议吗?适用于iOS! 感谢。

3 个答案:

答案 0 :(得分:0)

我有同样的问题。如果您在ListView项目中使用切换,请尝试删除开关。当我移除Switch时,我的问题就消失了。

我不知道您的特定情况,但我用图像替换开关

 var image = new Image {
        HorizontalOptions = LayoutOptions.Start,
 };
 image.SetBinding( Image.SourceProperty, new Binding( "Image" ) );
 image.WidthRequest = image.HeightRequest = 15;

图像绑定到这些属性的位置:

public static ImageSource Checked = ImageSource.FromResource( "checked.png" );
public static ImageSource Unchecked = ImageSource.FromResource( "unchecked.png" );
public ImageSource Image {
    get {
        if ( Selected ) {
            return Checked;
        } else {
            return Unchecked;
        }
    }
}

private bool m_Selected;
public bool Selected { 
    get{ 
        return m_Selected;
    }
    set{
        SetField(ref m_Selected, value, "Selected");
        OnPropertyChanged( "Image" );
    }
}

OnPropertyChange和SetField来自INotifyPropertyChanged成员:

public class SomeClass : INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged( string propertyName ) {
        PropertyChangedEventHandler handler = PropertyChanged;
        if ( handler != null ) handler( this, new PropertyChangedEventArgs( propertyName ) );
    }
    protected bool SetField<T>( ref T field, T value, string propertyName ) {
        if ( EqualityComparer<T>.Default.Equals( field, value ) ) return false;
        field = value;
        OnPropertyChanged( propertyName );
        return true;
    }
}

请注意如果Checked和Unchecked属性不是静态的,即使图像也会出现问题。

答案 1 :(得分:0)

尝试在OnAppearing函数中添加所有控件,并在OnDisappearing中将Content设置为null,如下所示:

public class MyPage : ContentPage
{
   public MyPage()
   {
      Title = "MyPage";
   }

   protected override void OnAppearing()
   {
      List<string> listItems = new List<string> { "a", "b", "c" };
      ListView listView = new ListView
      {
         ItemsSource = listItems,
         ItemTemplate = new DataTemplate(() =>
         {
            TextCell textCell = new TextCell();
            textCell.SetBinding(TextCell.TextProperty, ".");
            return textCell;
         })
     };

     StackLayout stackLayout = new StackLayout();
     stackLayout.Children.Add(listView);
     Content = stackLayout;

     base.OnAppearing();
  }

   protected override void OnDisappearing()
   {
      Content = null;
      base.OnDisappearing();
   }
}

详细说明可在blog.cloush.com

上找到

答案 2 :(得分:0)

我必须在我的案例中找到原因垃圾收集器帮我解决 await Task.Yield()是一个选项,有时它不起作用,在我的情况下它会在收集垃圾后解决

GC.collect();

在页面显示时删除缓存