Exceptions when using MvxAdapter

I have the following setup: Application with two tabs. In the second tab, the ListView is bound to an ObservableCollection. The first tab has controls that cause the ViewModel for the second tab to reload the ObservableCollection. It worked well until I added a custom MvxAdapter (needed for polymorphic list items). After that, after each update, I see the following exceptions in the application output (note that the list is still reloading its values):

    mvx:Warning: 42.24 Exception masked during Adapter RealNotifyDataSetChanged ArgumentException: 'jobject' must not be IntPtr.Zero.
Parameter name: jobject
      at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (IntPtr jobject, IntPtr jclass, IntPtr jmethod) [0x00010] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.14-series/8e79d361/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:866 
  at Android.Widget.BaseAdapter.NotifyDataSetChanged () [0x00058] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.14-series/8e79d361/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Widget.BaseAdapter.cs:311 
  at Cirrious.MvvmCross.Binding.Droid.Views.MvxAdapter.RealNotifyDataSetChanged () [0x00000] in <filename unknown>:0 
mvx:Warning: 42.24 Exception masked during Adapter RealNotifyDataSetChanged ArgumentException: 'jobject' must not be IntPtr.Zero.
Parameter name: jobject
      at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (IntPtr jobject, IntPtr jclass, IntPtr jmethod) [0x00010] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.14-series/8e79d361/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:866 
  at Android.Widget.BaseAdapter.NotifyDataSetChanged () [0x00058] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.14-series/8e79d361/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Widget.BaseAdapter.cs:311 
  at Cirrious.MvvmCross.Binding.Droid.Views.MvxAdapter.RealNotifyDataSetChanged () [0x00000] in <filename unknown>:0 

      

I wonder if anyone can help me find the reason why these exceptions are being thrown.

public override View OnCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
    {
        base.OnCreateView(inflater, container, savedInstanceState);

        var view = this.BindingInflate(Resource.Layout.ChildPage_History, null);

        listView = view.FindViewById<MvxListView> (Resource.Id.history_list);

        listView.Adapter = new CustomAdapter (this.Activity, (IMvxAndroidBindingContext)BindingContext);

        return view;
    }

      

+3


source to share


2 answers


I think the problem is caused MvxListView

by the default constructor being created MvxAdapter

. When bindings are created, by default MvxAdapter

subscribes to a collection change event for the collection to which the list is bound. However, when the parameter is MvxListView.Adapter

set to a value CustomAdapter

, the original MvxAdapter

is still subscribed to the collection. Ultimately, the original one MvxAdapter

gets garbage collected, so when the collection changes the handler is called with the located object and the specified exception is the result.

I solved the problem in my project by getting my own MvxListView

that overrides the constructor and returns the adapter, i.e. there is no need to set it in the OnCreateView:



public class MyListView : MvxListView
{
    public MyListView(Context context, IAttributeSet attrs) :
        base(context, attrs, new CustomAdapter(context))
    {
    }
}

      

Just change the XML format to use this class instead MvxListView

and the exceptions should stop.

+5


source


This is a threading problem. You have to pick up the source property change on the correct flow. Try downloading the source code in another project and then returning it to the property linked in the list.



+1


source







All Articles