Reputation: 47
So I've this ListView in my app which is instantiated once. 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.
My problem occurs if I
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! Thanks.
Upvotes: 1
Views: 999
Reputation: 338
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
GC.collect();
remove the cache when the page is displays
Upvotes: 0
Reputation: 1642
Try to add all your controls in the OnAppearing function and set Content to null in OnDisappearing like this:
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
Upvotes: 0
Reputation: 1646
I had the same issue. If you're using Switch inside the ListView's item, try removing the 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:
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.
Upvotes: 0