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

[Bug] System.IndexOutOfRangeException: 'Can't set CarouselView to position -1. ItemsSource has X items.' #14952

Open
npostma opened this issue Dec 3, 2021 · 15 comments
Labels
a/carouselview p/Android s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/unverified New report that has yet to be verified t/bug 🐛
Projects

Comments

@npostma
Copy link

npostma commented Dec 3, 2021

Description

After updating the ObservableCollection which is bound to CarouselView, rendering the page with the carousel view gives a crash. Android only. iOS works as expected.

Exception message: [Bug] System.IndexOutOfRangeException: 'Can't set CarouselView to position -1. ItemsSource has X items.'
X can be any positive number, its the count of the updated ObservableCollection.

Its a Xamarin exception. I do not call the Position property.

Steps to Reproduce

  1. Create a CarouselView in .xaml
  2. Bind the view to a ObservableCollection
  3. Modify the collection A or B.
    3.a .Clear() or
    3.b collection = new ObservableCollection();
  4. Open another page in the app. (I use an Xamarin with shell app)
  5. Go back to the page with the carousel. The app crashes.

I do not get a stack trace. So i have no clue where it originates.

Expected Behavior

That the app does not crash. Like it does in iOS. And the carousel shows the updated collection.

Actual Behavior

Basic Information

  • Version with issue:
  • Latest stable version: 5.0.0.2244
  • Last known good version:
  • Not sure.
  • Platform Target Frameworks:
    • Android: 11.0 (R)
  • NuGet Packages:
  • Next to the default packages, Skiasharp, Text.Json and TestFramework
  • Affected Devices:
  • All android devices ive tested (Nokia X20, Samsung S8, Nokia 7)

Environment

Microsoft Visual Studio Community 2019
Version 16.11.7
VisualStudio.16.Release/16.11.7+31911.196
Microsoft .NET Framework
Version 4.8.04084

Installed Version: Community

Visual C++ 2019   00435-60000-00000-AA636
Microsoft Visual C++ 2019

.NET Core Debugging with WSL   1.0
.NET Core Debugging with WSL

ADL Tools Service Provider   1.0
This package contains services used by Data Lake tools

ASA Service Provider   1.0

ASP.NET and Web Tools 2019   16.11.94.52318
ASP.NET and Web Tools 2019

ASP.NET Web Frameworks and Tools 2019   16.11.94.52318
For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0   16.11.94.52318
Azure App Service Tools v3.0.0

Azure Data Lake Node   1.0
This package contains the Data Lake integration nodes for Server Explorer.

Azure Data Lake Tools for Visual Studio   2.6.1000.0
Microsoft Azure Data Lake Tools for Visual Studio

Azure Functions and Web Jobs Tools   16.11.94.52318
Azure Functions and Web Jobs Tools

Azure Stream Analytics Tools for Visual Studio   2.6.1000.0
Microsoft Azure Stream Analytics Tools for Visual Studio

C# Tools   3.11.0-4.21403.6+ae1fff344d46976624e68ae17164e0607ab68b10
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools   1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Extensibility Message Bus   1.2.6 (master@34d6af2)
Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

Fabric.DiagnosticEvents   1.0
Fabric Diagnostic Events

IntelliCode Extension   1.0
IntelliCode Visual Studio Extension Detailed Info

Microsoft Azure HDInsight Azure Node   2.6.1000.0
HDInsight Node under Azure Node

Microsoft Azure Hive Query Language Service   2.6.1000.0
Language service for Hive query

Microsoft Azure Service Fabric Tools for Visual Studio   16.10
Microsoft Azure Service Fabric Tools for Visual Studio

Microsoft Azure Stream Analytics Language Service   2.6.1000.0
Language service for Azure Stream Analytics

Microsoft Azure Stream Analytics Node   1.0
Azure Stream Analytics Node under Azure Node

Microsoft Azure Tools for Visual Studio   2.9
Support for Azure Cloud Services projects

Microsoft Continuous Delivery Tools for Visual Studio   0.4
Simplifying the configuration of Azure DevOps pipelines from within the Visual Studio IDE.

Microsoft JVM Debugger   1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft Library Manager   2.1.113+g422d40002e.RR
Install client-side libraries easily to any web project

Microsoft MI-Based Debugger   1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards   1.0
Microsoft Visual C++ Wizards

Microsoft Visual Studio Tools for Containers   1.2
Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

Microsoft Visual Studio VC Package   1.0
Microsoft Visual Studio VC Package

Mono Debugging for Visual Studio   16.10.15 (552afdf)
Support for debugging Mono processes with Visual Studio.

NuGet Package Manager   5.11.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

ProjectServicesPackage Extension   1.0
ProjectServicesPackage Visual Studio Extension Detailed Info

Razor (ASP.NET Core)   16.1.0.2122504+13c05c96ea6bdbe550bd88b0bf6cdddf8cde1725
Provides languages services for ASP.NET Core Razor.

SQL Server Data Tools   16.0.62111.11070
Microsoft SQL Server Data Tools

Test Adapter for Boost.Test   1.0
Enables Visual Studio's testing tools with unit tests written for Boost.Test.  The use terms and Third Party Notices are available in the extension installation directory.

Test Adapter for Google Test   1.0
Enables Visual Studio's testing tools with unit tests written for Google Test.  The use terms and Third Party Notices are available in the extension installation directory.

ToolWindowHostedEditor   1.0
Hosting json editor into a tool window

TypeScript Tools   16.0.30526.2002
TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools   3.11.0-4.21403.6+ae1fff344d46976624e68ae17164e0607ab68b10
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual C++ for Cross Platform Mobile Development (Android)   16.0.31829.152
Visual C++ for Cross Platform Mobile Development (Android)

Visual F# Tools   16.11.0-beta.21514.6+b6c2c4f53ea3a08fa603ea93d6d2f808a62a21d1
Microsoft Visual F# Tools

Visual Studio Code Debug Adapter Host Package   1.0
Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Container Tools Extensions   1.0
View, manage, and diagnose containers within Visual Studio.

Visual Studio Tools for CMake   1.0
Visual Studio Tools for CMake

Visual Studio Tools for Containers   1.0
Visual Studio Tools for Containers

Visual Studio Tools for Kubernetes   1.0
Visual Studio Tools for Kubernetes

VisualStudio.DeviceLog   1.0
Information about my package

VisualStudio.Foo   1.0
Information about my package

VisualStudio.Mac   1.0
Mac Extension for Visual Studio

Xamarin   16.11.000.197 (d16-11@6934992)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer   16.11.0.47 (remotes/origin/d16-11@e0d612363)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates   16.10.5 (355b57a)
Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK   12.0.0.3 (d16-11/f0e3c2d)
Xamarin.Android Reference Assemblies and MSBuild support.
    Mono: c633fe9
    Java.Interop: xamarin/java.interop/d16-11@476bb5b
    ProGuard: Guardsquare/proguard/v7.0.1@912d149
    SQLite: xamarin/sqlite/3.35.4@85460d3
    Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-11@87af37b


Xamarin.iOS and Xamarin.Mac SDK   15.2.0.17 (738fde344)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

Code

My .XAML

<CarouselView 
                ItemsSource="{Binding CarouselItems}" 
                BackgroundColor="Transparent"
                HorizontalOptions="Center"
                VerticalOptions="Center"
                x:Name="carouselView"
                >

                <CarouselView.ItemTemplate
                    >
                    <DataTemplate
                        x:DataType="vme:CarouselItemViewModel">
                        <StackLayout>
                            <Frame 
                                BorderColor="DarkSlateGray"
                                CornerRadius="10"
                                HorizontalOptions="Center"
                                VerticalOptions="CenterAndExpand"
                                >
                                <StackLayout Opacity="{Binding Opacity}">
                                    <Image Source="{Binding Image.Source}">
                                        <Image.GestureRecognizers>
                                            <gesture:PressedGestureRecognizer Command="{Binding PressedGestureCommand}" />
                                            <gesture:ReleasedGestureRecognizer Command="{Binding ReleasedGestureCommand}" />
                                            <gesture:ReleaseCancelledGestureRecognizer Command="{Binding CancelledGestureCommand}" />
                                            <gesture:MovedGestureRecognizer Command="{Binding MovedGestureCommand}" />
                                        </Image.GestureRecognizers>
                                    </Image>
                                </StackLayout>
                            </Frame>
                        </StackLayout>
                    </DataTemplate>
                </CarouselView.ItemTemplate>
            </CarouselView>

Screenshots

N.a.

Workaround

Sadly no. No working workaround so far. Tried all kinds of things (like building it programmatically) but run into another issue where the carousel would not center and was always on top of the screen spite of being a 1:1 copy of the .XAML.

@npostma npostma added s/unverified New report that has yet to be verified t/bug 🐛 labels Dec 3, 2021
@jsuarezruiz
Copy link
Contributor

Could you attach a small sample where reproduce the issue?. Thanks in advance.

@jsuarezruiz jsuarezruiz added this to New in Triage via automation Dec 13, 2021
@jsuarezruiz jsuarezruiz added the s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. label Dec 13, 2021
@jsuarezruiz jsuarezruiz moved this from New to Needs Info in Triage Dec 13, 2021
@npostma
Copy link
Author

npostma commented Dec 17, 2021

I will extract the the code to an empty project and see if i can recreate this issue this week. tbc.

@npostma
Copy link
Author

npostma commented Dec 21, 2021

@jsuarezruiz I've created a working 'crash' demo.

https://github.com/npostma/bug_reports-CarouselViewAndroidCrash

[edit]
Updated to the latest nuget Xamarin forms and essentials package but the problem still exists.

@npostma
Copy link
Author

npostma commented Dec 24, 2021

Clearing the collection ( .Clear() ) in the example instead of re initializing results in the same crash.

@brettnguyen
Copy link

brettnguyen commented Feb 2, 2022

I believe I solved your problem. Put this in your carousel viewmodel:

public void OnDisappearing()
{
IsBusy = false;
}

then put this in your caurselpage.xaml.cs

protected override void OnDisappearing()
{
base.OnDisappearing();

viewModel.OnDisappearing();

ClearCarousel();
}

This should work I tested it.

@npostma
Copy link
Author

npostma commented Feb 3, 2022

What trickery is this :-) It seems to work. How can the IsBusy bool have effect on the collection?

@HomeroLara
Copy link

@jsuarezruiz Is there a better way to handle this exception than the solution that @brettnguyen suggested? ( thanks for the suggestion @brettnguyen )

@SylvainMoingeon
Copy link

Hello,

This is two Stacktraces that can help to debug the crash.

To reproduce the crash :

  1. Add CarouselView on a page & add data (itemsSource, binding...)
  2. Navigate to another page
  3. Reload data relative to CarouselView
  4. Go back to page with CarouselView

It seems that CarouselView doesn't like having its data refreshed when not displayed.

I created an Android CarouselViewRenderer and I catched the first exception in OnElementPropertyChanged method (the changed property is Position) :

System.IndexOutOfRangeException: Can't set CarouselView to position -1. ItemsSource has 3 items.
  at Xamarin.Forms.Platform.Android.CarouselViewRenderer.UpdateFromPosition () [0x0005d] in D:\a\1\s\Xamarin.Forms.Platform.Android\CollectionView\CarouselViewRenderer.cs:539 
  at Xamarin.Forms.Platform.Android.CarouselViewRenderer.OnElementPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs changedProperty) [0x00065] in D:\a\1\s\Xamarin.Forms.Platform.Android\CollectionView\CarouselViewRenderer.cs:169 
  at eStay.Mobile.Droid.Components.DebugCarouselViewRenderer.OnElementPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs changedProperty) [0x00046] in C:\Sources\EstayMobile\eStay.Mobile2\eStay.Mobile2.Android\Components\DebugCarouselViewRenderer.cs:50 

After exiting the try/catch, another exception, that I didn't achieve to catch, crash the app :

 **System.ArgumentOutOfRangeException:** 'Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index'
  at System.Collections.Generic.List`1[T].get_Item (System.Int32 index) [0x00009] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/List.cs:161 
 at System.Collections.Generic.List`1[T].System.Collections.IList.get_Item (System.Int32 index) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/List.cs:188 
 at Xamarin.Forms.Platform.Android.ObservableItemsSource.ElementAt (System.Int32 index) [0x0000f] in D:\a\1\s\Xamarin.Forms.Platform.Android\CollectionView\ObservableItemsSource.cs:226 
 at Xamarin.Forms.Platform.Android.ObservableItemsSource.GetItem (System.Int32 position) [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\CollectionView\ObservableItemsSource.cs:62 
 at Xamarin.Forms.Platform.Android.CarouselViewRenderer.SetCurrentItem (System.Int32 carouselPosition) [0x00043] in D:\a\1\s\Xamarin.Forms.Platform.Android\CollectionView\CarouselViewRenderer.cs:504 
 at Xamarin.Forms.Platform.Android.CarouselViewRenderer.UpdateInitialPosition () [0x00076] in D:\a\1\s\Xamarin.Forms.Platform.Android\CollectionView\CarouselViewRenderer.cs:362 
 at Xamarin.Forms.Platform.Android.CarouselViewRenderer.UpdateAdapter () [0x00069] in D:\a\1\s\Xamarin.Forms.Platform.Android\CollectionView\CarouselViewRenderer.cs:141 
 at Xamarin.Forms.Platform.Android.CarouselViewRenderer.UpdateItemsSource () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\CollectionView\CarouselViewRenderer.cs:151 
 at Xamarin.Forms.Platform.Android.ItemsViewRenderer`3[TItemsView,TAdapter,TItemsViewSource].SetUpNewElement (TItemsView newElement) [0x00053] in D:\a\1\s\Xamarin.Forms.Platform.Android\CollectionView\ItemsViewRenderer.cs:316 
 at Xamarin.Forms.Platform.Android.CarouselViewRenderer.SetUpNewElement (Xamarin.Forms.ItemsView newElement) [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\CollectionView\CarouselViewRenderer.cs:94 
 at Xamarin.Forms.Platform.Android.ItemsViewRenderer`3[TItemsView,TAdapter,TItemsViewSource].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x0004f] in D:\a\1\s\Xamarin.Forms.Platform.Android\CollectionView\ItemsViewRenderer.cs:104 
 at void android.os.Handler.handleCallback(android.os.Messa05-09 14:34:58.122 F/stay_app.mobil(18823): java_vm_ext.cc:578]   at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Content.Context context) [0x0003e] in D:\a\1\s\Xamarin.Forms.Platform.Android\Platform.cs:358 
 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) [0x000b3] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:140 
 at Xamarin.Forms.Platform.Android.VisualElementPackager.Load () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:112 
 at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetPackager (Xamarin.Forms.Platform.Android.VisualElementPackager packager) [0x00007] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:481 
 at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetElement (TElement element) [0x000e8] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:244 
 at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00033] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:131 
 at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Content.Context context) [0x0003e] in D:\a\1\s\Xamarin.Forms.Platform.Android\Platform.cs:358 
 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) [0x000b3] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:140 
 at Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement) [0x00163] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:351 
 at Xamarin.Forms.Platform.Android.VisualElementPackager.Load () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:112 
 at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetPackager (Xamarin.Forms.Platform.Android.VisualElementPackager packager) [0x00007] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:481 
 at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetElement (TElement element) [0x000e8] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:244 
 at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00033] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:131 
 at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Content.Context context) [0x0003e] in D:\a\1\s\Xamarin.Forms.Platform.Android\Platform.cs:358 
 at Xamarin.Forms.Platform.Android.ScrollViewContainer.set_ChildView (Xamarin.Forms.View value) [0x0002f] in D:\a\1\s\Xamarin.Forms.Platform.Android\Renderers\ScrollViewContainer.cs:35 
 at Xamarin.Forms.Platform.Android.ScrollViewRenderer.LoadContent () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\Renderers\ScrollViewRenderer.cs:369 
 at Xamarin.Forms.Platform.Android.ScrollViewRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x000cf] in D:\a\1\s\Xamarin.Forms.Platform.Android\Renderers\ScrollViewRenderer.cs:99 
 at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Content.Context context) [0x0003e] in D:\a\1\s\Xamarin.Forms.Platform.Android\Platform.cs:358 
 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) [0x000b3] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:140 
 at Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement) [0x00163] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:351 
 at Xamarin.Forms.Platform.Android.VisualElementPackager.Load () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:112 
 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) [0x00041] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:125 
 at Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement) [0x00163] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:351 
 at Xamarin.Forms.Platform.Android.VisualElementPackager.Load () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:112 
 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) [0x00041] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:125 
 at Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement) [0x00163] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:351 
 at Xamarin.Forms.Platform.Android.VisualElementPackager.Load () [0x00000] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:112 
 at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetPackager (Xamarin.Forms.Platform.Android.VisualElementPackager packager) [0x00007] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:481 
 at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetElement (TElement element) [0x000e8] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:244 
 at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00033] in D:\a\1\s\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:131 
 at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element, AndroidX.Fragment.App.FragmentManager fragmentManager, Android.Content.Context context) [0x00031] in D:\a\1\s\Xamarin.Forms.Platform.Android\Platform.cs:404 
 at Xamarin.Forms.Platform.Android.AppCompat.FragmentContainer.OnCreateView (Android.Views.LayoutInflater inflater, Android.Views.ViewGroup container, Android.OS.Bundle savedInstanceState) [0x00008] in D:\a\1\s\Xamarin.Forms.Platform.Android\AppCompat\FragmentContainer.cs:57 
 at AndroidX.Fragment.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_inflater, System.IntPtr native_container, System.IntPtr native_savedInstanceState) [0x00021] in D:\a\1\s\generated\androidx.fragment.fragment\obj\Release\monoandroid9.0\generated\src\AndroidX.Fragment.App.Fragment.cs:1977 
 at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.68(intptr,intptr,intptr,intptr,intptr)
 at android.view.View crc64720bb2db43a66fe9.FragmentContainer.n_onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) (FragmentContainer.java:-2)
 at android.view.View crc64720bb2db43a66fe9.FragmentContainer.onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) (FragmentContainer.java:41)
 at void androidx.fragment.app.Fragment.performCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle) (Fragment.java:2963)
 at void androidx.fragment.app.FragmentStateManager.createView() (FragmentStateManager.java:518)
 at void androidx.fragment.app.FragmentStateManager.moveToExpectedState() (FragmentStateManager.java:282)
 at void androidx.fragment.app.FragmentManager.executeOpsTogether(java.util.ArrayList, java.util.ArrayList, int, int) (FragmentManager.java:2189)
 at void androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(java.util.ArrayList, java.util.ArrayList) (FragmentManager.java:2100)
 at void androidx.fragment.app.FragmentManager.execSingleAction(androidx.fragment.app.FragmentManager$OpGenerator, boolean) (FragmentManager.java:1971)
 at void androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss() (BackStackRecord.java:311)
 at void androidx.fragment.app.FragmentPagerAdapter.finishUpdate(android.view.ViewGroup) (FragmentPagerAdapter.java:249)
 at void androidx.viewpager.widget.ViewPager.populate(int) (ViewPager.java:1244)
 at void androidx.viewpager.widget.ViewPager.setCurrentItemInternal(int, boolean, boolean, int) (ViewPager.java:669)
 at void androidx.viewpager.widget.ViewPager.setCurrentItemInternal(int, boolean, boolean) (ViewPager.java:631)
 at void androidx.viewpager.widget.ViewPager.setCurrentItem(int, boolean) (ViewPager.java:623)
 at boolean crc64720bb2db43a66fe9.TabbedPageRenderer.n_onNavigationItemSelected(android.view.MenuItem) (TabbedPageRenderer.java:-2)
 at boolean crc64720bb2db43a66fe9.TabbedPageRenderer.onNavigationItemSelected(android.view.MenuItem) (TabbedPageRenderer.java:129)
 at boolean com.google.android.material.navigation.NavigationBarView$1.onMenuItemSelected(androidx.appcompat.view.menu.MenuBuilder, android.view.MenuItem) (NavigationBarView.java:241)
 at boolean androidx.appcompat.view.menu.MenuBuilder.dispatchMenuItemSelected(androidx.appcompat.view.menu.MenuBuilder, android.view.MenuItem) (MenuBuilder.java:834)
 at boolean androidx.appcompat.view.menu.MenuItemImpl.invoke() (MenuItemImpl.java:158)
 at boolean androidx.appcompat.view.menu.MenuBuilder.performItemAction(android.view.MenuItem, androidx.appcompat.view.menu.MenuPresenter, int) (MenuBuilder.java:985)
 at void com.google.android.material.navigation.NavigationBarMenuView$1.onClick(android.view.View) (NavigationBarMenuView.java:110)
 at boolean android.view.View.performClick() (View.java:8160)
 at boolean android.view.View.performClickInternal() (View.java:8137)
 at boolean android.view.View.access$3700(android.view.View) (View.java:888)
 at void android.view.View$PerformClick.run() (View.java:30236)
 at void android.os.Handler.handleCallback(android.os.Message) (Handler.java:938)
 at void android.os.Handler.dispatchMessage(android.os.Message) (Handler.java:99)
 at void android.os.Looper.loop() (Looper.java:246)
 at void android.app.ActivityThread.main(java.lang.String[]) (ActivityThread.java:8653)
 at java.lang.Object java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object[]) (Method.java:-2)
 at void com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run() (RuntimeInit.java:602)
 at void com.android.internal.os.ZygoteInit.main(java.lang.String[]) (ZygoteInit.java:1130)
   in call to NewString
   from android.view.View crc64720bb2db43a66fe9.FragmentContainer.n_onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)

@matt-goldman
Copy link

@brettnguyen thanks for the workaround, worked for me!

@SolarisYan
Copy link

Just add follow code in caurselpage.xaml.cs will fix it, this is not related to IsBusy:

protected override void OnDisappearing()
{
    base.OnDisappearing();
    ClearCarousel();
}

@npostma
Copy link
Author

npostma commented Jul 29, 2022

@jsuarezruiz do you still need more info on this issue?

@Kiriakos41
Copy link

Anything?

@npostma
Copy link
Author

npostma commented Oct 6, 2022

Anything? This issue still exists and giving me trouble from time to time and need to do some funky workarrounds because the IsBusy = false / true; does not alway help.

@AmrAlSayed0
Copy link

AmrAlSayed0 commented Nov 27, 2023

This bug produces 3 stacktraces
I have overridden 3 methods in my CustomCarouselViewRenderer
This

protected override void UpdateAdapter()
{
	try
	{
		base.UpdateAdapter();
	}
	catch (ArgumentOutOfRangeException e)
	{
		System.Diagnostics.Debug.WriteLine(e));
	}
}

produces this

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
   at object System.Collections.Generic.List<object>.get_Item(int index) in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/List.cs:line 161
   at object System.Collections.Generic.List<object>.System.Collections.IList.get_Item(int index) in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/List.cs:line 188
   at object Xamarin.Forms.Platform.Android.ObservableItemsSource.ElementAt(int index)
   at object Xamarin.Forms.Platform.Android.ObservableItemsSource.GetItem(int position)
   at void Xamarin.Forms.Platform.Android.CarouselViewRenderer.SetCurrentItem(int carouselPosition)
   at void Xamarin.Forms.Platform.Android.CarouselViewRenderer.UpdateInitialPosition()
   at void Xamarin.Forms.Platform.Android.CarouselViewRenderer.UpdateAdapter()
   at void Renderers.CustomCarouselViewRenderer.UpdateAdapter() in D:/<>/Renderers/CustomCarouselViewRenderer.android.cs:line 34

This

protected override void SetUpNewElement(ItemsView newElement)
{
	try
	{
		base.SetUpNewElement(newElement);
	}
	catch (ArgumentOutOfRangeException e)
	{
		System.Diagnostics.Debug.WriteLine(e);
	}
}

produces this

System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
   at object System.Collections.Generic.List<object>.get_Item(int index) in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/List.cs:line 161
   at object System.Collections.Generic.List<object>.System.Collections.IList.get_Item(int index) in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/List.cs:line 188
   at object Xamarin.Forms.Platform.Android.ObservableItemsSource.ElementAt(int index)
   at object Xamarin.Forms.Platform.Android.ObservableItemsSource.GetItem(int position)
   at void Xamarin.Forms.Platform.Android.CarouselViewRenderer.SetCurrentItem(int carouselPosition)
   at void Xamarin.Forms.Platform.Android.CarouselViewRenderer.UpdateInitialPosition()
   at void Xamarin.Forms.Platform.Android.CarouselViewRenderer.SetUpNewElement(ItemsView newElement)
   at void Renderers.CustomCarouselViewRenderer.SetUpNewElement(ItemsView newElement) in D:/<>/CustomCarouselViewRenderer.android.cs:line 49

This

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs changedProperty)
{
	try
	{
		base.OnElementPropertyChanged(sender, changedProperty);
	}
	catch (IndexOutOfRangeException e)
	{
		System.Diagnostics.Debug.WriteLine(e);
	}
}

produces this

System.IndexOutOfRangeException: Can't set CarouselView to position -1. ItemsSource has 1 items.
   at void Xamarin.Forms.Platform.Android.CarouselViewRenderer.UpdateFromPosition()
   at void Xamarin.Forms.Platform.Android.CarouselViewRenderer.OnElementPropertyChanged(object sender, PropertyChangedEventArgs changedProperty)
   at void Renderers.CustomCarouselViewRenderer.OnElementPropertyChanged(object sender, PropertyChangedEventArgs changedProperty) in D:/<>/Renderer/CustomCarouselViewRenderer.android.cs:line 71

I think this mainly happens because the ItemSource is cleared (not set to null) while the CurrentItem is still holding the old item. One of the methods in the renderer, probably UpdateInitialPosition, tries to find the old item in the empty item source and produces the index -1 which causes the issue.

@flaubertagu
Copy link

The solution is to set the the carroussel in a view model.
I had the same issue and I solve it by doing that.
Just make sure that the PressedGestureRecognizer works as weel

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a/carouselview p/Android s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/unverified New report that has yet to be verified t/bug 🐛
Projects
Triage
  
Needs Info
Development

No branches or pull requests