Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[Bug, CollectionView] CollectionView crashes when ItemsSource updated too fast #9753

Closed
RonnyBansemer opened this issue Feb 27, 2020 · 17 comments
Assignees
Labels
a/collectionview Android10 i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often in-progress This issue has an associated pull request that may resolve it! m/high impact ⬛ p/Android t/bug 🐛

Comments

@RonnyBansemer
Copy link

RonnyBansemer commented Feb 27, 2020

Description

When CollectionView is bound to a ObservableCollection and it is updated before display fade in animation has finished it will crash.

Steps to Reproduce

  1. Create CollectionView and bind an ObservableCollection to ItemsSource
  2. Quickly add several items to ObservableCollection

Basic Information

  • Version with issue: 4.5.0.356
  • Last known good version: --
  • IDE: VS for Mac
  • Platform Target Frameworks:
    • Android: 9.1
  • Android Support Library Version: 10.0

Exception Log

Exception #0: Inconsistency detected. Invalid item position 48(offset:49).state:49 crc643f46942d9dd1fff9.CollectionViewRenderer{ba0eea9 VFED..... ......ID 0,0-394,652 #1d1}, adapter:crc643f46942d9dd1fff9.GroupableItemsViewAdapter_2@16bfd3a, layout:androidx.recyclerview.widget.LinearLayoutManager@3183dc7, context:android.view.ContextThemeWrapper@42bf230

  --- End of managed Java.Lang.IndexOutOfBoundsException stack trace ---
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 48(offset:49).state:49 crc643f46942d9dd1fff9.CollectionViewRenderer{ba0eea9 VFED..... ......ID 0,0-394,652 #1d1}, adapter:crc643f46942d9dd1fff9.GroupableItemsViewAdapter_2@16bfd3a, layout:androidx.recyclerview.widget.LinearLayoutManager@3183dc7, context:android.view.ContextThemeWrapper@42bf230
	at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6183)
	at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
	at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
	at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
	at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
	at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
	at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
	at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:4085)
	at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3849)
	at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1897)
	at androidx.recyclerview.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5260)
	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:933)
	at android.view.Choreographer.doCallbacks(Choreographer.java:742)
	at android.view.Choreographer.doFrame(Choreographer.java:671)
	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:919)
	at android.os.Handler.handleCallback(Handler.java:761)
	at android.os.Handler.dispatchMessage(Handler.java:98)
	at android.os.Looper.loop(Looper.java:156)
	at android.app.ActivityThread.main(ActivityThread.java:6623)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)

Workaround

-> Update all CollectionView changing properties on main thread.
-> Throttle elements added to observable collection interval by 250ms.

@RonnyBansemer RonnyBansemer added s/unverified New report that has yet to be verified t/bug 🐛 labels Feb 27, 2020
@pauldipietro pauldipietro added this to New in Triage Feb 27, 2020
@jsuarezruiz jsuarezruiz added a/collectionview Android10 i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often p/Android labels Feb 27, 2020
@jsuarezruiz
Copy link
Contributor

@RonnyBansemer Could you attach a repro sample?. I have been doing some tests to reproduce the issue but without success.
Issue9753.zip

@jsuarezruiz jsuarezruiz moved this from New to Needs Info in Triage Feb 27, 2020
@jsuarezruiz jsuarezruiz added the s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. label Feb 27, 2020
@RonnyBansemer
Copy link
Author

RonnyBansemer commented Feb 27, 2020

@jsuarezruiz Sure, I tested long time, because I could not reproduce it with your repository as well. The Issue only seems to occour randomly when not consequently adding/updating collection elements on mainthread.
Issue9753.zip

@RonnyBansemer
Copy link
Author

RonnyBansemer commented Mar 2, 2020

@hs79hs As already mentioned, update ItemsSource of CollectionView and all properties of bound model on mainthread. Otherwise you possibly run into a race condition, because different threads try to update the CollectionView at same time. And then: BOOM, you get an exception.

@samhouts samhouts removed the s/needs-repro ❔ This reported issue doesn't include a sample project reproducing the issue. Please provide one. label Mar 2, 2020
brminnick added a commit to brminnick/GitTrends that referenced this issue Mar 3, 2020
@samhouts samhouts moved this from Needs Info to New in Triage Mar 3, 2020
@hartez hartez removed the s/unverified New report that has yet to be verified label Mar 4, 2020
@hartez
Copy link
Contributor

hartez commented Mar 4, 2020

If you take the repro from #9753 (comment) and change OnAddItemExecuted to

async Task OnAddItemExecuted(object item)
{
    var data = ((string)item).Split(',');

    await Task.Delay(TimeSpan.FromMilliseconds(Random.Next(10, 250)));
    Items.Add(new Product
    {
        Height = Random.Next(40, 150),
        Description = data[0],
        Number = data[1],
        Quantity = 1,
        Price = decimal.Parse(data[2])
    });
}

a couple seconds of rapid clicking will crash it every time.

@hartez hartez added this to Backlog in CollectionView via automation Mar 4, 2020
@hartez hartez removed this from New in Triage Mar 4, 2020
@RonnyBansemer
Copy link
Author

@hartez Again, this is a threading problem. Updating all ui changing properties solve the issue. Is the expected behavior to be able to update and change from random thread without crash?

@hs79hs
Copy link

hs79hs commented Mar 4, 2020

@RonnyBansemer I tried and yes, if put all UI related work in main thread, this issue never happened. Thanks, this solved my problem.

@samhouts samhouts moved this from Backlog to To do (blockers) in CollectionView Mar 13, 2020
@samhouts samhouts moved this from To do (blockers) to Backlog in CollectionView Mar 13, 2020
@hartez hartez self-assigned this Mar 16, 2020
@ederjbezerra
Copy link

ederjbezerra commented Apr 23, 2020

I have tried adding items on main thread and it works fine BUT if you use ItemsUpdatingScrollMode="KeepLastItemInView" in XAML, the application crashs with exception:

Java.Lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 229(offset:231).state:231 crc643f46942d9dd1fff9.CollectionViewRenderer{b16867d VFED.V... ......ID 20,20-1420,1932 #8}, adapter:crc643f46942d9dd1fff9.GroupableItemsViewAdapter_2@9983bec, layout:android.support.v7.widget.LinearLayoutManager@a998a6c, context:android.view.ContextThemeWrapper@2474d4
  at java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 229(offset:231).state:231 crc643f46942d9dd1fff9.CollectionViewRenderer{b16867d VFED.V... ......ID 20,20-1420,1932 #8}, adapter:crc643f46942d9dd1fff9.GroupableItemsViewAdapter_2@9983bec, layout:android.support.v7.widget.LinearLayoutManager@a998a6c, context:android.view.ContextThemeWrapper@2474d4
  at at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5923)
  at at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
  at at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
  at at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
  at at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
  at at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
  at at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
  at at android.support.v7.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:3875)
  at at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3639)
  at at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1888)
  at at android.support.v7.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5044)
  at at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927)
  at at android.view.Choreographer.doCallbacks(Choreographer.java:702)
  at at android.view.Choreographer.doFrame(Choreographer.java:635)
  at at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913)
  at at android.os.Handler.handleCallback(Handler.java:751)
  at at android.os.Handler.dispatchMessage(Handler.java:95)
  at at android.os.Looper.loop(Looper.java:154)
  at at android.app.ActivityThread.main(ActivityThread.java:6682)
  at at java.lang.reflect.Method.invoke(Native Method)
  at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
  at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

if you remove it from xaml, works nice but without the auto scrolling resource.

@txarito
Copy link

txarito commented May 6, 2020

@hs79hs As already mentioned, update ItemsSource of CollectionView and all properties of bound model on mainthread. Otherwise you possibly run into a race condition, because different threads try to update the CollectionView at same time. And then: BOOM, you get an exception.

For me Device.BeginInvokeOnMainThread on collection manipulation solve crash. Delay doens',t work.

Thanks

@inimirpaz
Copy link

inimirpaz commented Jun 19, 2020

Similar issue here when adding/updating/removing items in observed collection (ItemSource of the CollectionView). The exception is "Java.Lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionTemplatedItemViewHolder".

[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] Java.Lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionTemplatedItemViewHolder{7860c34 position=3 id=-1, oldPos=2, pLpos:2 scrap [attachedScrap] tmpDetached not recyclable(1) no parent} crc643f46942d9dd1fff9.CollectionViewRenderer{70072e9 VFED..... ......ID 0,171-1080,1585 #a8}, adapter:crc643f46942d9dd1fff9.GroupableItemsViewAdapter_2@a7d829c, layout:androidx.recyclerview.widget.GridLayoutManager@fa62a46, context:android.view.ContextThemeWrapper@181df70
[MonoDroid]   at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x0008e] in <dac4c5a4b77f4e61a5e6d9d3050dfb9f>:0 
[MonoDroid]   at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0005d] in <dac4c5a4b77f4e61a5e6d9d3050dfb9f>:0 
[MonoDroid]   at AndroidX.RecyclerView.Widget.RecyclerView.OnLayout (System.Boolean changed, System.Int32 l, System.Int32 t, System.Int32 r, System.Int32 b) [0x0006f] in D:\a\1\s\generated\androidx.recyclerview.recyclerview\obj\Release\monoandroid90\generated\src\AndroidX.RecyclerView.Widget.RecyclerView.cs:13220 
[MonoDroid]   at Xamarin.Forms.Platform.Android.ItemsViewRenderer`3[TItemsView,TAdapter,TItemsViewSource].OnLayout (System.Boolean changed, System.Int32 l, System.Int32 t, System.Int32 r, System.Int32 b) [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\CollectionView\ItemsViewRenderer.cs:68 
[MonoDroid]   at AndroidX.RecyclerView.Widget.RecyclerView.n_OnLayout_ZIIII (System.IntPtr jnienv, System.IntPtr native__this, System.Boolean changed, System.Int32 l, System.Int32 t, System.Int32 r, System.Int32 b) [0x00008] in D:\a\1\s\generated\androidx.recyclerview.recyclerview\obj\Release\monoandroid90\generated\src\AndroidX.RecyclerView.Widget.RecyclerView.cs:13204 
[MonoDroid]   at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.107(intptr,intptr,bool,int,int,int,int)
[MonoDroid]   --- End of managed Java.Lang.IndexOutOfBoundsException stack trace ---
[MonoDroid] java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionTemplatedItemViewHolder{7860c34 position=3 id=-1, oldPos=2, pLpos:2 scrap [attachedScrap] tmpDetached not recyclable(1) no parent} crc643f46942d9dd1fff9.CollectionViewRenderer{70072e9 VFED..... ......ID 0,171-1080,1585 #a8}, adapter:crc643f46942d9dd1fff9.GroupableItemsViewAdapter_2@a7d829c, layout:androidx.recyclerview.widget.GridLayoutManager@fa62a46, context:android.view.ContextThemeWrapper@181df70
[MonoDroid] 	at androidx.recyclerview.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5974)
[MonoDroid] 	at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6158)
[MonoDroid] 	at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
[MonoDroid] 	at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
[MonoDroid] 	at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
[MonoDroid] 	at androidx.recyclerview.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:561)
[MonoDroid] 	at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
[MonoDroid] 	at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
[MonoDroid] 	at androidx.recyclerview.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
[MonoDroid] 	at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep1(RecyclerView.java:4085)
[MonoDroid] 	at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3849)
[MonoDroid] 	at android.view.View.layout(View06-19 09:27:04.958 I/MonoDroid(18116): 	at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
[MonoDroid] 	at android.view.ViewRootImpl$TraversalRunnable.run(ViewRo06-19 09:27:04.958 I/MonoDroid(18116): 	at crc643f46942d9dd1fff9.ItemsViewRenderer_3.n_onLayout(Native Method)
[MonoDroid] 	at crc643f46942d9dd1fff9.ItemsViewRenderer_3.onLayout(ItemsViewRenderer_3.java:45)
[MonoDroid] 	at android.view.View.layout(View.java:20672)
[MonoDroid] 	at android.view.ViewGroup.layout(ViewGroup.java:6194)
[MonoDroid] 	at crc643f46942d9dd1fff9.Platform_DefaultRenderer.n_onLayout(Native Method)
[MonoDroid] 	at crc643f46942d9dd1fff9.Platform_DefaultRenderer.onLayout(Platform_DefaultRenderer.java:72)
[MonoDroid] 	at android.view.View.layout(View.java:20672)
[MonoDroid] 	at android.view.ViewGroup.layout(ViewGroup.java:6194)
[MonoDroid] 	at crc643f46942d9dd1fff9.Platform_DefaultRenderer.n_onLayout(Native Method)
[MonoDroid] 	at crc643f46942d9dd1fff9.Platform_DefaultRenderer.onLayout(Platform_DefaultRenderer.java:72)
[MonoDroid] 	at android.view.View.layout(View.java:20672)
[MonoDroid] 	at android.view.ViewGroup.layout(ViewGroup.java:6194)
[MonoDroid] 	at com.xamarin.forms.platform.android.FormsViewGroup.measureAndLayout(FormsViewGroup.java:37)
[MonoDroid] 	at crc643f46942d9dd1fff9.PageRenderer.n_onLayout(Native Method)
[MonoDroid] 	at crc643f46942d9dd1fff9.PageRenderer.onLayout(PageRenderer.java:72)
[MonoDroid] 	at android.view.View.layout(View.java:20672)
[MonoDroid] 	at android.view.ViewGroup.layout(ViewGroup.java:6194)
[MonoDroid] 	at com.xamarin.forms.platform.android.FormsViewGroup.measureAndLayout(FormsViewGroup.java:37)
[MonoDroid] 	at crc643f46942d9dd1fff9.ShellPageContainer.n_onLayout(Native Method)
[MonoDroid] 	at crc643f46942d9dd1fff9.ShellPageContainer.onLayout(ShellPageContainer.java:53)
[MonoDroid] 	at android.view.View.layout(View.java:20672)
[MonoDroid] 	at android.view.ViewGroup.layout(ViewGroup.java:6194)
[MonoDroid] 	at com.google.android.material.appbar.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:142)
[MonoDroid] 	at com.google.android.material.appbar.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:41)
[MonoDroid] 	at com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1556)
[MonoDroid] 	at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:918)
[MonoDroid] 	at android.view.View.layout(View.java:20672)
[MonoDroid] 	at android.view.ViewGroup.layout(ViewGroup.java:6194)
[MonoDroid] 	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
[MonoDroid] 	at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
[MonoDroid] 	at android.view.View.layout(View.java:20672)
[MonoDroid] 	at android.view.ViewGroup.layout(ViewGroup.java:6194)
[MonoDroid] 	at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
[MonoDroid] 	at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
[MonoDroid] 	at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
[MonoDroid] 	at android.view.View.layout(View.java:20672)
[MonoDroid] 	at android.view.ViewGroup.layout(ViewGroup.java:6194)
[MonoDroid] 	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
[MonoDroid] 	at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
[MonoDroid] 	at android.view.View.layout(View.java:20672)
[MonoDroid] 	at android.view.ViewGroup.layout(ViewGroup.java:6194)
[MonoDroid] 	at androidx.drawerlayout.widget.DrawerLayout.onLayout(DrawerLayout.java:1231)
[MonoDroid] 	at android.view.View.layout(View.java:20672)
[MonoDroid] 	at android.view.ViewGroup.layout(ViewGroup.java:6194)
[MonoDroid] 	at crc643f46942d9dd1fff9.PlatformRenderer.n_onLayout(Native Method)
[MonoDroid] 	at crc643f46942d9dd1fff9.PlatformRenderer.onLayout(PlatformRenderer.java:63)
[MonoDroid] 	at android.view.View.layout(View.java:20672)
[MonoDroid] 	at android.view.ViewGroup.layout(ViewGroup.java:6194)
[MonoDroid] 	at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
[MonoDroid] 	at android.view.View.layout(View.java:20672)
[MonoDroid] 	at android.view.ViewGroup.layout(ViewGroup.java:6194)
[MonoDroid] 	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
[MonoDroid] 	at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
[MonoDroid] 	at android.view.View.layout(View.java:20672)
[MonoDroid] 	at android.view.ViewGroup.layout(ViewGroup.java:6194)
[MonoDroid] 	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
[MonoDroid] 	at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
[MonoDroid] 	at android.view.View.layout(View.java:20672)
[MonoDroid] 	at android.view.ViewGroup.layout(ViewGroup.java:6194)
[MonoDroid] 	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
[MonoDroid] 	at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
[MonoDroid] 	at android.view.View.layout(View.java:20672)
[MonoDroid] 	at android.view.ViewGroup.layout(ViewGroup.java:6194)
[MonoDroid] 	at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
[MonoDroid] 	at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
[MonoDroid] 	at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
[MonoDroid] 	at android.view.View.layout(View.java:20672)
[MonoDroid] 	at android.view.ViewGroup.layout(ViewGroup.java:6194)
[MonoDroid] 	at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
[MonoDroid] 	at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
[MonoDroid] 	at com.android.internal.policy.DecorView.onLayout(DecorView.java:753)
[MonoDroid] 	at android.view.View.layout(View.java:20672)
[MonoDroid] 	at android.view.ViewGroup.layout(ViewGroup.java:6194)
[MonoDroid] 	at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2792)
[MonoDroid] 	at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2319)
[MonoDroid] 	at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
[MonoDroid] 	at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
[MonoDroid] 	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
[MonoDroid] 	at android.view.Choreographer.doCallbacks(Choreographer.java:761)
[MonoDroid] 	at android.view.Choreographer.doFrame(Choreographer.java:696)
[MonoDroid] 	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
[MonoDroid] 	at android.os.Handler.handleCallback(Handler.java:873)
[MonoDroid] 	at android.os.Handler.dispatchMessage(Handler.java:99)
[MonoDroid] 	at android.os.Looper.loop(Looper.java:193)
[MonoDroid] 	at android.app.ActivityThread.main(ActivityThread.java:6669)
[MonoDroid] 	at java.lang.reflect.Method.invoke(Native Method)
[MonoDroid] 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
[MonoDroid] 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
[MonoDroid] 
[Mono] DllImport searching in: '__Internal' ('(null)').
[Mono] Searching for 'java_interop_jnienv_throw'.
[Mono] Probing 'java_interop_jnienv_throw'.
[Mono] Found as 'java_interop_jnienv_throw'.
[.test.test_app] java_vm_ext.cc:545] JNI DETECTED ERROR IN APPLICATION: JNI CallObjectMethodA called with pending exception java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionTemplatedItemViewHolder{7860c34 position=3 id=-1, oldPos=2, pLpos:2 scrap [attachedScrap] tmpDetached not recyclable(1) no parent} crc643f46942d9dd1fff9.CollectionViewRenderer{70072e9 VFED..... ......ID 0,171-1080,1585 #a8}, adapter:crc643f46942d9dd1fff9.GroupableItemsViewAdapter_2@a7d829c, layout:androidx.recyclerview.widget.GridLayoutManager@fa62a46, context:android.view.ContextThemeWrapper@181df70
[.test.test_app] jav06-19 09:27:05.384 F/.test.test_app(18116): java_vm_ext.cc:545] (Throwable with no stack trace)
[.test.test_app] java_vm_ext.cc:545] 
[.test.test_app] java_vm_ext.cc:545]     in call to CallObjectMethodA
[.test.i06-19 09:27:05.384 F] java_vm_ext.cc:545] "main" prio=5 tid=1 Runnable
[.test.test_app] java_vm_ext.cc:545]   | group="main" sCount=0 dsCount=0 flags=0 obj=0x74e99ee0 self=0xf05f4000
[.test.test_app] java_vm_ext.cc:545]   | sysTid=18116 nice=-10 cgrp=default sched=0/0 handle=0xf51ba494
[.test.test_app] java_vm_ext.cc:545]   | state=R schedstat=( 21627228939 6749814554 12612 ) utm=1990 stm=172 core=1 HZ=100
[.test.test_app] java_vm_ext.cc:545]   | stack=0xff365000-0xff367000 stackSize=8MB
[.test.test_app] java_vm_ext.cc:545]   | held mutexes= "mutator lock"(shared held)
[.test.test_app] java_vm_ext.cc:545]   at android.view.ViewGroup.layout(ViewGroup.java:6 void*, bool)+198)
[.test.test_app] java_vm_ext.cc:545]   native: #01 pc 0051034e  /system/lib/libart.so (art::Thread::DumpStack(std::__1::basic_ostream<char, std::__1::char_traits<char>>&, bool, BacktraceMap*, bool) const+382)
[.test.test_app] java_vm_ext.cc:545]   native: #03 pc 0031a720  /system/lib/libart.so (art::JavaVMExt::JniAbort(char const*, char const*)+1088)
[.test.test_app] java_vm_ext.cc:545]   native: #04 pc 0031ab91  /system/lib/libart.so (art::JavaVMExt::JniAbortV(char const*, char const*, char*)+113)
[.test.test_app] java_vm_ext.cc:545]   native: #05 pc 000d5f77  /system/lib/libart.so (art::(anonymous namespace)::ScopedCheck::AbortF(char const*, ...)+71)
[.test.test_app] java_vm_ext.cc:545]   native: #07 pc 000d3a5b  /system/lib/libart.so (art::(anonymous namespace)::ScopedCheck::Check(art::ScopedObjectAccess&, bool, char const*, art::(anonymous namespace)::JniValueType*)+811)
[.test.test_app] java_vm_ext.cc:545]   at and06-19 09:27:05.384 F/.test.test_app(18116): java_vm_ext.cc:545]   native: #08 pc 000d9758  /system/lib/libart.so (art::(anonymous namespace)::CheckJNI::CheckCallArgs(art::ScopedObjectAccess&, art::(anonymous namespace)::ScopedCheck&, _JNIEnv*, _jobject*, _jclass*, _jmethodID*, art::InvokeType, art::(anonymous namespace)::VarArgs const*)+200)
[.test.test_app] java_vm_ext.cc:545]   native: #10 pc 000c3f29  /system/lib/libart.so (art::(anonymous namespace)::CheckJNI::CallObjectMethodA(_JNIEnv*, _jobject*, _jmethodID*, jvalue*)+73)
[.test.test_app] java_vm_ext.cc:545]   at android.widget.LinearLayout.layoutVertical(06-19 09:27:05.384 F/.test.test_app(18116): java_vm_ext.cc:545]   native: #11 pc 000100b8  /data/app/biz.test.test_appe-sqFo3WSkBLTuz8VxWGwung==/lib/x86/libmonodroid.so (java_interop_jnienv_call_object_method_a+56)
[.test.test_app] java_vm_ext.cc:545]   native: #12 pc 0004c817  <anonymous:ccba4000> (???)
[.test.test_app] java_vm_ext.cc:545]   at crc643f46942d9dd1fff9.ItemsViewRenderer_3.n_onLayout(Native method)
[.test.test_app] java_vm_ext.cc:545]   at crc643f46942d9dd1fff9.ItemsViewRenderer_3.onLayout(ItemsViewRenderer_3.java:45)
[.test.test_app] java_vm_ext.cc:545]   at android.view.ViewGroup.layout(ViewGroup.java:6194)
[.test.test_app] java_vm_ext.cc:545]   at crc643f46942d9dd1fff9.Platform_DefaultRenderer.n_onLayout(Native method)
[.test.test_app] java_vm_ext.cc:545]   at crc643f46942d9dd1fff9.Platform_DefaultRenderer.onLayout(Platform_DefaultRenderer.java:72)
[.test.test_app] java_vm_ext.cc:545]   at android.view.View.layout(View.java:20672)
[.test.test_app] java_vm_ext.cc:545]   at android.view.ViewGroup.layout(ViewGroup.java:6194)
[.test.test_app] java_vm_ext.cc:545]   at android.view.Choreographer06-19 09:27:05.385 F/.test.test_app(18116): java_vm_ext.cc:545]   at com.xamarin.forms.platform.android.FormsViewGroup.measureAndLayout(FormsViewGroup.java:37)
[.test.test_app] java_vm_ext.cc:545]   at crc643f46942d9dd1fff9.Platform_DefaultRenderer.n_onLayout(Native method)
[.test.test_app] java_vm_ext.cc:545]   at android.view.View.layout(View.java:20672)
[.test.test_app] java_vm_ext.cc:545]   at android.view.ViewGroup.layout(ViewGroup.java:6194)
[.test.test_app] java_vm_ext.cc:545]   at com.xamarin.forms.platform.android.FormsViewGroup.measureAndLayout(FormsViewGroup.java:37)
[.test.test_app] java_vm_ext.cc:545]   at crc643f46942d9dd1fff9.PageRenderer.n_onLayout(Native method)
[.test.test_app] java_vm_ext.cc:545]   at crc643f46942d9dd1fff9.PageRenderer.onLayout(PageRenderer.java:72)
[.test.test_app] java_vm_ext.cc:545]   at android.view.View.layout(View.java:20672)
[.test.test_app] java_vm_ext.cc:545]   at android.view.ViewGroup.layout(ViewGroup.java:6194)
[.test.test_app] java_vm_ext.cc:545]   at com.xamarin.forms.platform.android.FormsViewGroup.measureAndLayout(FormsViewGroup.java:37)
[.test.test_app] java_vm_ext.cc:545]   at crc643f46942d9dd1fff9.ShellPageContainer.n_onLayout(Native method)
[.test.test_app] java_vm_ext.cc:545]   at crc643f46942d9dd1fff9.ShellPageContainer.onLayout(ShellPageContainer.java:53)
[.test.test_app] java_vm_ext.cc:545]   at android.view.View.layout(View.java:20672)
[.test.test_app] java_vm_ext.cc:545]   at android.view.ViewGroup.layout(ViewGroup.java:6194)
[.test.test_app] java_vm_ext.cc:545]   at com.google.android.material.appbar.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:142)
[.test.test_app] java_vm_ext.cc:545]   at com.google.android.material.appbar.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:41)
[.test.test_app] java_vm_ext.cc:545]   at com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1556)
[.test.test_app] java_vm_ext.cc:545]   at androidx.coordinatorlayout.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:918)
[.test.test_app] java_vm_ext.cc:545]   at android.view.View.layout(View.java:20672)
[.test.test_app] java_vm_ext.cc:545]   at android.view.ViewGroup.layout(ViewGroup.java:6194)
[.test.test_app] java_vm_ext.cc:545]   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
[.test.test_app] java_vm_ext.cc:545]   at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
[.test.test_app] java_vm_ext.cc:545]   at android.view.View.layout(View.java:20672)
[.test.test_app] java_vm_ext.cc:545]   at android.view.ViewGroup.layout(ViewGroup.java:6194)
[.test.test_app] java_vm_ext.cc:545]   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
[.test.test_app] java_vm_ext.cc:545]   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
[.test.test_app] java_vm_ext.cc:545]   at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
[.test.test_app] java_vm_ext.cc:545]   at android.view.View.layout(View.java:20672)
[.test.test_app] java_vm_ext.cc:545]   at android.view.ViewGroup.layout(ViewGroup.java:6194)
[.test.test_app] java_vm_ext.cc:545]   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
[.test.test_app] java_vm_ext.cc:545]   at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
[.test.test_app] java_vm_ext.cc:545]   at android.view.View.layout(View.java:20672)
[.test.test_app] java_vm_ext.cc:545]   at android.view.ViewGroup.layout(ViewGroup.java:6194)
[.test.test_app] java_vm_ext.cc:545]   at androidx.drawerlayout.widget.DrawerLayout.onLayout(DrawerLayout.java:1231)
[.test.test_app] java_vm_ext.cc:545]   at android.view.View.layout(View.java:20672)
[.test.test_app] java_vm_ext.cc:545]   at android.view.ViewGroup.layout(ViewGroup.java:6194)
[.test.test_app] java_vm_ext.cc:545]   at crc643f46942d9dd1fff9.PlatformRenderer.n_onLayout(Native method)
[.test.test_app] java_vm_ext.cc:545]   at crc643f46942d9dd1fff9.PlatformRenderer.onLayout(PlatformRenderer.java:63)
[.test.test_app] java_vm_ext.cc:545]   at android.view.View.layout(View.java:20672)
[.test.test_app] java_vm_ext.cc:545]   at android.view.ViewGroup.layout(ViewGroup.java:6194)
[.test.test_app] java_vm_ext.cc:545]   at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1083)
[.test.test_app] java_vm_ext.cc:545]   at android.view.View.layout(View.java:20672)
[.test.test_app] java_vm_ext.cc:545]   at android.view.ViewGroup.layout(ViewGroup.java:6194)
[.test.test_app] java_vm_ext.cc:545]   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
[.test.test_app] java_vm_ext.cc:545]   at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
[.test.test_app] java_vm_ext.cc:545]   at android.view.View.layout(View.java:20672)
[.test.test_app] java_vm_ext.cc:545]   at android.view.ViewGroup.layout(ViewGroup.java:6194)
[.test.test_app] java_vm_ext.cc:545]   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
[.test.test_app] java_vm_ext.cc:545]   at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
[.test.test_app] java_vm_ext.cc:545]   at android.view.View.layout(View.java:20672)
[.test.test_app] java_vm_ext.cc:545]   at android.view.ViewGroup.layout(ViewGroup.java:6194)
[.test.test_app] java_vm_ext.cc:545]   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
[.test.test_app] java_vm_ext.cc:545]   at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
[.test.test_app] java_vm_ext.cc:545]   at android.view.View.layout(View.java:20672)
[.test.test_app] java_vm_ext.cc:545]   at android.view.ViewGroup.layout(ViewGroup.java:6194)
[.test.test_app] java_vm_ext.cc:545]   at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
[.test.test_app] java_vm_ext.cc:545]   at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
[.test.test_app] java_vm_ext.cc:545]   at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
[.test.test_app] java_vm_ext.cc:545]   at android.view.View.layout(View.java:20672)
[.test.test_app] java_vm_ext.cc:545]   at android.view.ViewGroup.layout(ViewGroup.java:6194)
[.test.test_app] java_vm_ext.cc:545]   at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
[.test.test_app] java_vm_ext.cc:545]   at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
[.test.test_app] java_vm_ext.cc:545]   at com.android.internal.policy.DecorView.onLayout(DecorView.java:753)
[.test.test_app] java_vm_ext.cc:545]   at android.view.View.layout(View.java:20672)
[.test.test_app] java_vm_ext.cc:545]   at android.view.ViewGroup.layout(ViewGroup.java:6194)
[.test.test_app] java_vm_ext.cc:545]   at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2792)
[.test.test_app] java_vm_ext.cc:545]   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2319)
[.test.test_app] java_vm_ext.cc:545]   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
[.test.test_app] java_vm_ext.cc:545]   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
[.test.test_app] java_vm_ext.cc:545]   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
[.test.test_app] java_vm_ext.cc:545]   at android.view.Choreographer.doCallbacks(Choreographer.java:761)
[.test.test_app] java_vm_ext.cc:545]   at android.view.Choreographer.doFrame(Choreographer.java:696)
[.test.test_app] java_vm_ext.cc:545]   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
[.test.test_app] java_vm_ext.cc:545]   at android.os.Handler.handleCallback(Handler.java:873)
[.test.test_app] java_vm_ext.cc:545]   at android.os.Handler.dispatchMessage(Handler.java:99)
[.test.test_app] java_vm_ext.cc:545]   at android.os.Looper.loop(Looper.java:193)
[.test.test_app] java_vm_ext.cc:545]   at android.app.ActivityThread.main(ActivityThread.java:6669)
[.test.test_app] java_vm_ext.cc:545]   at java.lang.reflect.Method.invoke(Native method)
[.test.test_app] java_vm_ext.cc:545]   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
[.test.test_app] java_vm_ext.cc:545]   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
[.test.test_app] java_vm_ext.cc:545] 

Another "workaround" to be added to the list is to: create a copy of the observed collection, clear the observed collection, work on the copy and at the end add back to the observed from the copy like so:

public ObservableCollection<object> YourItemSource { get; private set; }
[...]
ObservableCollection<object> tmpItemSource = new ObservableCollection<object>(YourItemSource);
YourItemSource.Clear();
[...]
foreach (object o in tmpItemSource)
   YourItemSource.Add(o);

Still a waste of time/resources though.

@eli191
Copy link

eli191 commented Jun 24, 2020

@inimirpaz I have the same crash as you, but using a temporary collection does not resolve it :( I want to update the collection items.

@hartez
Copy link
Contributor

hartez commented Jun 24, 2020

This is the same core issue as reported in #10735.

There are actually two different bugs demonstrated here:

  1. Adding items off the main thread results in an invalid view holder.
  2. Adding items off the main thread results in an invalid item position if you have KeepLastItemInView set.

We have a fix in progress (#10765), but we're still reviewing it. In the meantime, as has been mentioned upthread, you can work around the first issue by making sure that you are on the main thread when adding items to your ItemsSource. You can do this by chekcing Device.IsInvokeRequired and using Device.BeginInvokeOnMainThread.

The second issue may require a separate fix; I'll be taking a look at that tomorrow.

@eli191
Copy link

eli191 commented Jun 24, 2020

@hartez What I have is Java.Lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionTemplatedItemViewHolder and I am already updating the ObservableCollection on the main thread inside Device.BeginInvokeOnMainThread so that fix is not relevant for me.

@samhouts samhouts added the in-progress This issue has an associated pull request that may resolve it! label Jun 29, 2020
@samhouts samhouts added this to In Progress in 4.7.0 Jun 29, 2020
@samhouts samhouts moved this from Backlog to In Progress in CollectionView Jun 29, 2020
@misavidal
Copy link

@inimirpaz I have the same crash as you, but using a temporary collection does not resolve it :( I want to update the collection items.

The below workaround solve my problem, maybe you want try:
(*using the example of inimirpaz)

public ObservableCollection YourItemSource { get; private set; }
[...]
ObservableCollection tmpItemSource = new ObservableCollection(YourItemSource);
YourItemSource.Clear();
[...]

Instead of:
foreach (object o in tmpItemSource)
YourItemSource.Add(o);

I'm using:
YourItemSource = tmpItemSource;

@samhouts samhouts removed this from In Progress in 4.7.0 Jul 20, 2020
CollectionView automation moved this from In Progress to Done Jul 21, 2020
@Cacuci
Copy link

Cacuci commented Jul 29, 2020

I have the same problem. What's the solution?

@brminnick
Copy link
Contributor

@Cacuci The fix was done in this Pull Request: ab55162

And it is available in Xamarin.Forms v4.8.0-Pre2

Screen Shot 2020-07-29 at 12 58 11 PM

@samhouts samhouts added this to In Progress in vCurrent (4.8.0) Jul 30, 2020
@samhouts samhouts moved this from In Progress to Done in vCurrent (4.8.0) Jul 30, 2020
@samhouts samhouts removed this from Done in CollectionView Aug 20, 2020
@ish-1313
Copy link

ish-1313 commented Dec 17, 2020

seems that just hit the same or related problem. at least workaround helped
xamarin.forms 4.8.0.1687
android

page setup
2 pickers and 2 radiobuttons binded to viewmodel
method, which called from pickers binded properties and one of radiobutton binded property
where data from local list filtered according to above controls values and then assigned to property Events
which is binded to CollectionView ItemsSource

app hungs (somewhere outside of my code) on second switch of radiobuttons

sorted it out with temporary collection
ObservableCollection<cPetEvent> result = null;
and
Device.BeginInvokeOnMainThread(() => Events = result);

the same problem was on this page with 2 checkboxes, and continues after checkboxes were replaced with radiobuttons

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a/collectionview Android10 i/high Completely doesn't work, crashes, or is unusably slow, has no obvious workaround; occurs less often in-progress This issue has an associated pull request that may resolve it! m/high impact ⬛ p/Android t/bug 🐛
Projects
No open projects
Development

No branches or pull requests