我们开发了一个Xamarin.Android应用程序,该应用程序在首页(我们称为HomeActivityFragment)上包含一个截面为RecyclerView的部分(改名为https://gist.github.com/gabrielemariotti/4c189fb1124df4556058#file-simplesectionedrecyclerviewadapter-java),当我们单击列表中的某个项目时,它会重定向到一个ActivityFragment(我们将其称为WorkOrderActivityFragment),其中也包含RecyclerView。
这是问题所在:当我们在WorkOrderActivityFragment上时,当我们单击“返回”按钮(在工具栏中)时,它是Activity.OnBackPressed()=>它应该回到HomeActivityFragment =>它可以工作...但是有时会引发异常:此示例代码中的ObjectDisposedException:
public class WorkOrderHeaderAdapter : RecyclerView.Adapter
{
private SparseArray<Section> Sections { get; } = new SparseArray<Section>();
internal MeasuringPointAdapter BaseAdapter { get; set; }
public WorkOrderHeaderAdapter(MeasuringPointAdapter baseAdapter)
{
BaseAdapter = baseAdapter;
}
public override int ItemCount => BaseAdapter.ItemCount + Sections.Size(); <-- the exception is thrown on this line, especially on "Sections" (SparseArray object)
// overriden methods
}
似乎我遇到了同样的问题,但是在另一个适配器(相似的代码,也使用SparseArray对象)中
不知道这是与Xamarin相关的问题,还是我们忘记正确处理SparseArray对象,但是我们没有找到解决方法:(
这是我详细介绍的例外:
01-09 11:05:04.443 I/MonoDroid(27122): UNHANDLED EXCEPTION:
01-09 11:05:04.496 I/MonoDroid(27122): System.ObjectDisposedException: Cannot access a disposed object.
01-09 11:05:04.496 I/MonoDroid(27122): Object name: 'Android.Util.SparseArray`1[[Com.Mobile.LibraryCommon.Android.HomeSectionsItems.Section, Com.Mobile.LibraryCommon.Android, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'.
01-09 11:05:04.496 I/MonoDroid(27122): at Java.Interop.JniPeerMembers.AssertSelf (Java.Interop.IJavaPeerable self) [0x00029] in <0ad2222fd7074badb5de547b1521aab0>:0
01-09 11:05:04.496 I/MonoDroid(27122): at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualInt32Method (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00000] in <0ad2222fd7074badb5de547b1521aab0>:0
01-09 11:05:04.496 I/MonoDroid(27122): at Android.Util.SparseArray.Size () [0x0000a] in <ad2f15102b3a4d36b40e9b0cbc11c376>:0
01-09 11:05:04.496 I/MonoDroid(27122): at MyWO.Android.Adapter.Workorder.WorkOrderHeaderAdapter.get_ItemCount () [0x0000d] in D:\MyWO.Mobile\MyWO\MyWO.Android\Adapter\Workorder\WorkOrderHeaderAdapter.cs:27
01-09 11:05:04.496 I/MonoDroid(27122): at Android.Support.V7.Widget.RecyclerView+Adapter.n_GetItemCount (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in <7cd821e41c6245e986359cd08947256b>:0
01-09 11:05:04.496 I/MonoDroid(27122): at (wrapper dynamic-method) System.Object.35(intptr,intptr)