知道 Android 上 Xamarin.Forms 中的非托管代码可能导致此 java.util.concurrentModificationException 的原因是什么吗?

Any idea what could be causing this java.util.concurrentModificationException from unmanaged code in Xamarin.Forms on Android?

我不断从我的 Xamarin.Forms 应用程序的 Android 项目中随机收到以下异常。

java.util.ConcurrentModificationException: 
  at java.util.ArrayList$Itr.next (ArrayList.java:860)
  at com.telerik.widget.primitives.panels.RadScrollView.notifyLaidOutListeners (RadScrollView.java:153)
  at com.telerik.widget.primitives.panels.RadScrollView.onLayout (RadScrollView.java:2092)
  at android.view.View.layout (View.java:24475)
  at android.view.ViewGroup.layout (ViewGroup.java:7383)
  at crc643f46942d9dd1fff9.ViewRenderer_2.n_onLayout (Native Method)
  at crc643f46942d9dd1fff9.ViewRenderer_2.onLayout (ViewRenderer_2.java:47)
  at android.view.View.layout (View.java:24475)
  at android.view.ViewGroup.layout (ViewGroup.java:7383)
  at com.xamarin.forms.platform.android.FormsViewGroup.measureAndLayout (FormsViewGroup.java:37)
  at crc643f46942d9dd1fff9.Platform_DefaultRenderer.n_onLayout (Native Method)
  at crc643f46942d9dd1fff9.Platform_DefaultRenderer.onLayout (Platform_DefaultRenderer.java:72)
  at android.view.View.layout (View.java:24475)
  at android.view.ViewGroup.layout (ViewGroup.java:7383)
  at com.xamarin.forms.platform.android.FormsViewGroup.measureAndLayout (FormsViewGroup.java:37)
  at crc643f46942d9dd1fff9.Platform_DefaultRenderer.n_onLayout (Native Method)
  at crc643f46942d9dd1fff9.Platform_DefaultRenderer.onLayout (Platform_DefaultRenderer.java:72)
  at android.view.View.layout (View.java:24475)
  at android.view.ViewGroup.layout (ViewGroup.java:7383)
  at com.xamarin.forms.platform.android.FormsViewGroup.measureAndLayout (FormsViewGroup.java:37)
  at crc643f46942d9dd1fff9.Platform_DefaultRenderer.n_onLayout (Native Method)
  at crc643f46942d9dd1fff9.Platform_DefaultRenderer.onLayout (Platform_DefaultRenderer.java:72)
  at android.view.View.layout (View.java:24475)
  at android.view.ViewGroup.layout (ViewGroup.java:7383)
  at com.xamarin.forms.platform.android.FormsViewGroup.measureAndLayout (FormsViewGroup.java:37)
  at crc64ee486da937c010f4.FrameRenderer.n_onLayout (Native Method)
  at crc64ee486da937c010f4.FrameRenderer.onLayout (FrameRenderer.java:47)
  at android.view.View.layout (View.java:24475)
  at android.view.ViewGroup.layout (ViewGroup.java:7383)
  at crc643f46942d9dd1fff9.Platform_DefaultRenderer.n_onLayout (Native Method)
  at crc643f46942d9dd1fff9.Platform_DefaultRenderer.onLayout (Platform_DefaultRenderer.java:72)
  at android.view.View.layout (View.java:24475)
  at android.view.ViewGroup.layout (ViewGroup.java:7383)
  at com.xamarin.forms.platform.android.FormsViewGroup.measureAndLayout (FormsViewGroup.java:37)
  at crc643f46942d9dd1fff9.Platform_DefaultRenderer.n_onLayout (Native Method)
  at crc643f46942d9dd1fff9.Platform_DefaultRenderer.onLayout (Platform_DefaultRenderer.java:72)
  at android.view.View.layout (View.java:24475)
  at android.view.ViewGroup.layout (ViewGroup.java:7383)
  at com.xamarin.forms.platform.android.FormsViewGroup.measureAndLayout (FormsViewGroup.java:37)
  at crc643f46942d9dd1fff9.ScrollViewContainer.n_onLayout (Native Method)
  at crc643f46942d9dd1fff9.ScrollViewContainer.onLayout (ScrollViewContainer.java:46)
  at android.view.View.layout (View.java:24475)
  at android.view.ViewGroup.layout (ViewGroup.java:7383)
  at android.widget.FrameLayout.layoutChildren (FrameLayout.java:332)
  at android.widget.FrameLayout.onLayout (FrameLayout.java:270)
  at androidx.core.widget.NestedScrollView.onLayout (NestedScrollView.java:1854)
  at crc643f46942d9dd1fff9.ScrollViewRenderer.n_onLayout (Native Method)
  at crc643f46942d9dd1fff9.ScrollViewRenderer.onLayout (ScrollViewRenderer.java:91)
  at android.view.View.layout (View.java:24475)
  at android.view.ViewGroup.layout (ViewGroup.java:7383)
  at crc643f46942d9dd1fff9.Platform_DefaultRenderer.n_onLayout (Native Method)
  at crc643f46942d9dd1fff9.Platform_DefaultRenderer.onLayout (Platform_DefaultRenderer.java:72)
  at android.view.View.layout (View.java:24475)
  at android.view.ViewGroup.layout (ViewGroup.java:7383)
  at com.xamarin.forms.platform.android.FormsViewGroup.measureAndLayout (FormsViewGroup.java:37)
  at crc643f46942d9dd1fff9.PageRenderer.n_onLayout (Native Method)
  at crc643f46942d9dd1fff9.PageRenderer.onLayout (PageRenderer.java:72)
  at android.view.View.layout (View.java:24475)
  at android.view.ViewGroup.layout (ViewGroup.java:7383)
  at com.xamarin.forms.platform.android.FormsViewGroup.measureAndLayout (FormsViewGroup.java:37)
  at crc64720bb2db43a66fe9.NavigationPageRenderer.n_onLayout (Native Method)
  at crc64720bb2db43a66fe9.NavigationPageRenderer.onLayout (NavigationPageRenderer.java:65)
  at android.view.View.layout (View.java:24475)
  at android.view.ViewGroup.layout (ViewGroup.java:7383)
  at com.xamarin.forms.platform.android.FormsViewGroup.measureAndLayout (FormsViewGroup.java:37)
  at crc64720bb2db43a66fe9.MasterDetailContainer.n_onLayout (Native Method)
  at crc64720bb2db43a66fe9.MasterDetailContainer.onLayout (MasterDetailContainer.java:46)
  at android.view.View.layout (View.java:24475)
  at android.view.ViewGroup.layout (ViewGroup.java:7383)
  at androidx.drawerlayout.widget.DrawerLayout.onLayout (DrawerLayout.java:1231)
  at crc64720bb2db43a66fe9.MasterDetailPageRenderer.n_onLayout (Native Method)
  at crc64720bb2db43a66fe9.MasterDetailPageRenderer.onLayout (MasterDetailPageRenderer.java:68)
  at android.view.View.layout (View.java:24475)
  at android.view.ViewGroup.layout (ViewGroup.java:7383)
  at crc643f46942d9dd1fff9.PlatformRenderer.n_onLayout (Native Method)
  at crc643f46942d9dd1fff9.PlatformRenderer.onLayout (PlatformRenderer.java:55)
  at android.view.View.layout (View.java:24475)
  at android.view.ViewGroup.layout (ViewGroup.java:7383)
  at android.widget.RelativeLayout.onLayout (RelativeLayout.java:1103)
  at android.view.View.layout (View.java:24475)
  at android.view.ViewGroup.layout (ViewGroup.java:7383)
  at android.widget.FrameLayout.layoutChildren (FrameLayout.java:332)
  at android.widget.FrameLayout.onLayout (FrameLayout.java:270)
  at android.view.View.layout (View.java:24475)
  at android.view.ViewGroup.layout (ViewGroup.java:7383)
  at android.widget.FrameLayout.layoutChildren (FrameLayout.java:332)
  at android.widget.FrameLayout.onLayout (FrameLayout.java:270)
  at android.view.View.layout (View.java:24475)
  at android.view.ViewGroup.layout (ViewGroup.java:7383)
  at android.widget.FrameLayout.layoutChildren (FrameLayout.java:332)
  at android.widget.FrameLayout.onLayout (FrameLayout.java:270)
  at android.view.View.layout (View.java:24475)
  at android.view.ViewGroup.layout (ViewGroup.java:7383)
  at android.widget.LinearLayout.setChildFrame (LinearLayout.java:1829)
  at android.widget.LinearLayout.layoutVertical (LinearLayout.java:1673)
  at android.widget.LinearLayout.onLayout (LinearLayout.java:1582)
  at android.view.View.layout (View.java:24475)
  at android.view.ViewGroup.layout (ViewGroup.java:7383)
  at android.widget.FrameLayout.layoutChildren (FrameLayout.java:332)
  at android.widget.FrameLayout.onLayout (FrameLayout.java:270)
  at com.android.internal.policy.DecorView.onLayout (DecorView.java:1225)
  at android.view.View.layout (View.java:24475)
  at android.view.ViewGroup.layout (ViewGroup.java:7383)
  at android.view.ViewRootImpl.performLayout (ViewRootImpl.java:4260)
  at android.view.ViewRootImpl.performTraversals (ViewRootImpl.java:3695)
  at android.view.ViewRootImpl.doTraversal (ViewRootImpl.java:2618)
  at android.view.ViewRootImpl$TraversalRunnable.run (ViewRootImpl.java:9971)
  at android.view.Choreographer$CallbackRecord.run (Choreographer.java:1010)
  at android.view.Choreographer.doCallbacks (Choreographer.java:809)
  at android.view.Choreographer.doFrame (Choreographer.java:744)
  at android.view.Choreographer$FrameDisplayEventReceiver.run (Choreographer.java:995)
  at android.os.Handler.handleCallback (Handler.java:938)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loop (Looper.java:246)
  at android.app.ActivityThread.main (ActivityThread.java:8538)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:602)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1130)

这似乎是随机发生的,我只是希望有人遇到过同样的问题并且知道该怎么做?我无法调试,因为它来自非托管代码。任何关于可能导致此问题的想法和任何潜在的解决方案都将非常感谢。

谢谢

由于你只提供错误信息,从错误信息来看,这个错误一般出现在ArrayList.

的迭代中

主要原因是modCountexpectedModCount的值调用list.remove()方法不一致

如果你使用迭代器这个错误可以通过直接调用Iterator.remove()来避免。

如果只是调用API,没有其他操作,应该是内部接口有类似问题。

经过大量调试后发现它与 可水平滚动的控件 (Telerik DataGrid) 可垂直滚动的布局 (Scrollview)[=41] 中有关=] 然后尝试在 Android 上使用 ScrollToAsync(),当 DataGrid 中的条目获得焦点时。

为了解决这个问题,在我的 OnAppearing() 重写中,我检查了 Device.RuntimePlatform 是 Android 并通过使用我的视图模型中的 IsVisible 绑定 属性 检查 DataGrid Control 是否应该可见,以及然后将其删除。此控件位于网格内,因此我能够使用控件的 x:Names 并调用 .Children.Remove() 来执行此操作.

然后使用 Device.RuntimePlatform 检查设备是否为 Android 以及是否为 ScrollToAsync() 没有被调用。

所以代码看起来像这样:

var vm = BindingContext as MyViewModelName;
if (Device.RuntimePlatform == Device.Android && !vm.DataGridIsVisible){
    MyGridControl.Children.Remove(MyDataGridControl);
}

注意这里的MyGridControlMyDataGridControl是控件的X:Name属性。