xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

Android crash from FrameRenderer caused by VisualElementPackager #5614

Open sis2004 opened 5 years ago

sis2004 commented 5 years ago

Description

When I use CarouselView inside a ListView (where each cell has its own carousel view), it crashes in an Android emulator with the following exception:

System.ArgumentException: Element must be of type Frame
  at Xamarin.Forms.Platform.Android.FastRenderers.FrameRenderer.Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00010] in C:\Projects\Repos\Xamarin.Forms\Xamarin.Forms.Platform.Android\FastRenderers\FrameRenderer.cs:83 
  at Xamarin.Forms.Platform.Android.VisualElementPackager.AddChild (Xamarin.Forms.VisualElement view, Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer, Xamarin.Forms.Platform.Android.RendererPool pool, System.Boolean sameChildren) [0x00130] in C:\Projects\Repos\Xamarin.Forms\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:127 
... see attached file for the full stack trace ...

Full stack trace

When I debugged it using a local Xamarin.Forms nuget package built from the latest master, I noticed that the children of the CarouselView (newChildren) were not in the same order as the corresponding renderers (_childViews) in the VisualElementPackager.SetElement method, which caused the FrameRenderer to be applied to the ContentView that resulted in this exception.

The image below illustrates this issue, where the Frame-based arrow controls and the indicator are the last 3 children, while their (frame) renderers are the first 3 elements in the _childViews.

CardViewFrameRenderer

Do you have any ideas on what's causing this, and how it can be fixed?

Note: This issue has been reported in the corresponding CardView repository, and the author agreed that it seems to be a Xamarin.Forms bug.

Steps to Reproduce

  1. Use CarouselView from the CardView package as an item template of a ListView. Include arrow controls and indicator view for the CarouselView.
  2. Display the form with that list view showing multiple carousel views and multiple items for each carousel view.
  3. Scroll up and down for the List view and left and right for each carousel view.

Expected Behavior

The form should scroll through items without crashing.

Actual Behavior

After some swiping the app crashes with the exception above.

Basic Information

AndreiMisiukevich commented 5 years ago

Similar issue https://github.com/AndreiMisiukevich/ContextMenu/issues/4

android.runtime.JavaProxyThrowable: System.InvalidCastException: Specified cast is not valid. at Xamarin.Forms.Platform.Android.ScrollViewRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00007] in <7999cc4bd5664eca8a72469344172ca3>:0 at Xamarin.Forms.Platform.Android.VisualElementPackager.AddChild

StackLayout is passed instead of scrollView.

AbuMandour commented 4 years ago

any update ?