简体   繁体   English

Xamarin.forms Android ListView-'jobject'不得为IntPtr.Zero

[英]Xamarin.forms Android ListView - 'jobject' must not be IntPtr.Zero

So I've this ListView in my app which is instantiated once. 所以我在应用程序中实例化了这个ListView。 The ListItem source is a List where I've a TemplateSelector which displays different cells depending on what kind of object that is stored in the list. ListItem源是一个列表,其中有一个TemplateSelector,它根据存储在列表中的对象类型显示不同的单元格。

My problem occurs if I 如果我发生问题

  1. Select a row in the list 在列表中选择一行
  2. Pop to previous Page 弹出上一页
  3. Push to my Page where I've my list 推送到我的列表所在的页面
  4. Select an item in the list 在列表中选择一个项目
  5. Crash. 崩溃。

I get the following error, 我收到以下错误,

[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>

Any suggestions? 有什么建议么? Works fine on iOS! 在iOS上运作良好! Thanks. 谢谢。

I had the same issue. 我遇到过同样的问题。 If you're using Switch inside the ListView's item, try removing the Switch . 如果您在ListView的项中使用Switch ,请尝试删除Switch My issue disappeared when I removed the Switch. 卸下交换机后,我的问题消失了。

I don't know your particular scenario but I substituted the switch with image 我不知道您的特定情况,但我用图片替换了开关

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

Where the Image is bound to these properties: 图像绑定到这些属性的位置:

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" );
    }
}

Where OnPropertyChange and SetField are from INotifyPropertyChanged members: 其中,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;
    }
}

Note that if the Checked and Unchecked properties are not static the problem appears even with an image . 请注意, 如果Checked和Unchecked属性不是静态的,则即使出现图像也会出现问题

Try to add all your controls in the OnAppearing function and set Content to null in OnDisappearing like this: 尝试在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();
   }
}

A detailed description can be found on blog.cloush.com 可以在blog.cloush.com上找到详细说明。

It is very had to find the reason in my case garbage collector help me to resolve await Task.Yield() is an option some time it don't working, in my case it resolve after collect the garbage 在我的案例中,非常有必要找出原因,垃圾收集器可以帮助我解决await Task.Yield()是某个选项,在某些时候它不起作用,就我而言,它是在收集垃圾之后解决的

GC.collect();

remove the cache when the page is displays 显示页面时删除缓存

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM