xamarin / Xamarin.Forms

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

[Bug] [Android] CarouselView.CurrentItem = null Causes App to Crash #13296

Open SkyeHoefling opened 3 years ago

SkyeHoefling commented 3 years ago

Description

When using a CarouselView if you attempt to set the CurrentItem to null the application crashes. The following code should not cause the application to crash.

Items.Clear();
CurrentItem = null;

Steps to Reproduce

  1. Open reproduction app
  2. Tap Clear

Expected Behavior

The app should allow the CurrentItem to be set to null as there is no longer a current item. An alternative would be once the ItemsSource is cleared the CurrentItem is automatically set to null.

Actual Behavior

The app crashes

Basic Information

Environment

Show/Hide Visual Studio info ``` Microsoft Visual Studio Enterprise 2019 Version 16.8.3 VisualStudio.16.Release/16.8.3+30804.86 Microsoft .NET Framework Version 4.8.04084 Installed Version: Enterprise Architecture Diagrams and Analysis Tools 00435-60000-00000-AA321 Microsoft Architecture Diagrams and Analysis Tools Visual C++ 2019 00435-60000-00000-AA321 Microsoft Visual C++ 2019 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.8.554.20160 ASP.NET and Web Tools 2019 ASP.NET Core Razor Language Services 16.1.0.2052803+84e121f1403378489b842e1797df2f3f5a49ac3c Provides languages services for ASP.NET Core Razor. ASP.NET Web Frameworks and Tools 2019 16.8.554.20160 For additional information, visit https://www.asp.net/ Azure App Service Tools v3.0.0 16.8.554.20160 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.8.554.20160 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.8.0-5.20604.10+9ed4b774d20940880de8df1ca8b07508aa01c8cd 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. Cookiecutter 16.8.20241.2 Provides tools for finding, instantiating and customizing templates in cookiecutter format. Dotfuscator Community Edition 5.42.0.9514-e0e25f754 PreEmptive Protection - Dotfuscator CE 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 GitHub.VisualStudio 2.11.102.28613 A Visual Studio Extension that brings the GitHub Flow into Visual Studio. 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.0 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 2.9 Microsoft Azure Tools for Microsoft Visual Studio 2019 - v2.9.30924.1 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.1 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.8.43 (00471f8) Support for debugging Mono processes with Visual Studio. Node.js Tools 1.5.20902.1 Commit Hash:b474efcb6f92db52a8f8e2e6a8cb9648476885cc Adds support for developing and debugging Node.js apps in Visual Studio NuGet Package Manager 5.8.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/ Office Developer Tools for Visual Studio 16.0.30502.00 Microsoft Office Developer Tools for Visual Studio ProjectServicesPackage Extension 1.0 ProjectServicesPackage Visual Studio Extension Detailed Info Python 16.8.20241.2 Provides IntelliSense, projects, templates, debugging, interactive windows, and other support for Python developers. Python - Conda support 16.8.20241.2 Conda support for Python projects. Python - Django support 16.8.20241.2 Provides templates and integration for the Django web framework. Python - IronPython support 16.8.20241.2 Provides templates and integration for IronPython-based projects. Python - Profiling support 16.8.20241.2 Profiling support for Python projects. Snapshot Debugging Extension 1.0 Snapshot Debugging Visual Studio Extension Detailed Info SQL Server Data Tools 16.0.62010.06180 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.21016.2001 TypeScript Tools for Microsoft Visual Studio Visual Basic Tools 3.8.0-5.20604.10+9ed4b774d20940880de8df1ca8b07508aa01c8cd 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.30608.117 Visual C++ for Cross Platform Mobile Development (Android) Visual C++ for Cross Platform Mobile Development (iOS) 16.0.30608.117 Visual C++ for Cross Platform Mobile Development (iOS) Visual C++ for Linux Development 1.0.9.30608 Visual C++ for Linux Development Visual F# Tools 16.8.0-beta.20507.4+da6be68280c89131cdba2045525b80890401defd 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 Workflow Manager Tools 1.0 1.0 This package contains the necessary Visual Studio integration components for Workflow Manager. Xamarin 16.8.000.260 (d16-8@fd405a2) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android. Xamarin Designer 16.8.0.507 (remotes/origin/d16-8@e87b24884) Visual Studio extension to enable Xamarin Designer tools in Visual Studio. Xamarin Templates 16.8.112 (86385a3) Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms. Xamarin.Android SDK 11.1.0.17 (d16-8/c0e2b8e) Xamarin.Android Reference Assemblies and MSBuild support. Mono: be2226b Java.Interop: xamarin/java.interop/d16-8@79d9533 ProGuard: Guardsquare/proguard/proguard6.2.2@ebe9000 SQLite: xamarin/sqlite/3.32.1@1a3276b Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-8@2fb1cbc Xamarin.iOS and Xamarin.Mac SDK 14.6.0.15 (87a1b18d8) Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support. ```

Build Logs

Not Applicable

Screenshots

Not Applicable

Reproduction Link

CVCurrentItemBug.zip

Workaround

My workaround is pretty hacky, but it works. In the ViewModel every time the SelectedItem is updated it publishes an event for the UI to update. Since I want to null out the SelectedItem when the Items has a length of 0, I can add a check for this. Only publish the event if the Items.Length > 0. This will allow the state of our ViewModel to properly reflect the data but only communicate to the UI if the it needs to be notified.

string selectedItem;
public string SelectedItem
{
    get => selectedItem;
    set
    {
        if (selectedItem == value)
            return;

        if (Items.Count > 0)
            RaisePropertyChanged();
    }
}
SkyeHoefling commented 3 years ago

I just confirmed that this is fixed if I update the XF project to the newly released 5.0.0.1874. Could we apply a fix for a 4.8 Service Pack?

The original reproduction project was fixed in 5.0.0.1874, however if you use IsSwipeEnabled it still crashes. See comment below https://github.com/xamarin/Xamarin.Forms/issues/13296#issuecomment-756172036

hartez commented 3 years ago

@ahoefling Any reason you can't update to 5.0?

SkyeHoefling commented 3 years ago

The short answer is yes I can upgrade to 5.0, but it wasn't exactly clear if this would work as the preview builds of 5.0 and even the initial stable release caused us problems.

When using the CarouselView in XF 4.8 I had the following usage

<CarouselView
    ItemsSource="{Binding Items}"
    CurrentItem="{Binding CurrentItem}"
    IsSwipeEnabled="True" />

In XF 4.8 by setting IsSwipeEnabled="True" it prevented the CarouselView from looping infinitely. When I upgraded the solution to XF 5.0, I noticed that a new property called Loop was added, by default this is set to true. In my scenario the CarouselView would dynamically add items and update the position on the carousel each time an item is added. It can also update the CurrentItem binded property to change the position. When I upgraded to XF 5.0 the CarouselView virtualization was acting 'funky' and inconsistent compared to XF 4.8. The best way to describe this is the CarouselView would slide to the wrong item or the same item would be duplicated a couple times. This would go away after a complete scroll of all items in the CarouselView.

When I updated the loop property to Loop="False", this problem went away.

<CarouselView
    ItemsSource="{Binding Items}"
    CurrentItem="{Binding CurrentItem}"
    IsSwipeEnabled="True"
    Loop="False" />

I spent a couple hours this morning trying to reproduce the in a small scale sample app, but wasn't able to get the same effect. I think my project adds additional moving parts on our specific Android device - Zebra TC70x which uses a custom AOSP fork of Android 8.1 that integrates with a hardware barcode scanner. Each item in the CarouselView is updated by barcode scans that originate from a background thread, I manually marshal these threads to the MainUI thread using Device.BeginInvokeOnMainThread().

I unfortunately did not have enough concrete data to create another issue for XF 5.0.

SkyeHoefling commented 3 years ago

Update - Crashes in XF 5.0

@hartez I did some more research on this and it is in fact happening in Xamarin.Forms 5.0. I originally thought this was fixed as the original sample code was crashing in XF 4.8 and working in XF 5.0. When I updated my proprietary code, I noticed I was still getting the crash. This still originated from the statement CarouselView.CurrentItem = null;. I believe part of the problem was resolved from XF 4.8 to XF 5.0, but not everything.

In short when you null out the CarouselView.CurrentItem if you have IsSwipeEnabled="True"`` XF will attempt an animation using theScrollTo()method of theItemsView`. Since there is nothing to scroll to, the application crashes

<CarouselView 
    ItemsSource="{Binding Items}"
    CurrentItem="{Binding SelectedItem}"
    Loop="False"
    IsSwipeEnabled="True" />
Items.Clear();
SelectedItem = null;

Stacktrace

ERROR: Invalid target position
    at Java.Interop.JniEnvironment+InstanceMethods.CallVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x0006e] in <89755ea61d9c4ae0a40ce90b872c9e2d>:0
    at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeVirtualVoidMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x0002a] in <89755ea61d9c4ae0a40ce90b872c9e2d>:0
    at AndroidX.RecyclerView.Widget.RecyclerView+LayoutManager.StartSmoothScroll (AndroidX.RecyclerView.Widget.RecyclerView+SmoothScroller smoothScroller) [0x00027] in D:\a\1\s\generated\androidx.recyclerview.recyclerview\obj\Release\monoandroid90\generated\src\AndroidX.RecyclerView.Widget.RecyclerView.cs:7510
    at Xamarin.Forms.Platform.Android.ScrollHelper.AnimateScrollToPosition (System.Int32 index, Xamarin.Forms.ScrollToPosition scrollToPosition) [0x00029] in D:\a\1\s\Xamarin.Forms.Platform.Android\CollectionView\ScrollHelper.cs:62
    at Xamarin.Forms.Platform.Android.CarouselViewRenderer.ScrollTo (Xamarin.Forms.ScrollToRequestEventArgs args) [0x00037] in D:\a\1\s\Xamarin.Forms.Platform.Android\CollectionView\CarouselViewRenderer.cs:219
    at Xamarin.Forms.Platform.Android.ItemsViewRenderer`3[TItemsView,TAdapter,TItemsViewSource].ScrollToRequested (System.Object sender, Xamarin.Forms.ScrollToRequestEventArgs args) [0x00022] in D:\a\1\s\Xamarin.Forms.Platform.Android\CollectionView\ItemsViewRenderer.cs:603
    at Xamarin.Forms.ItemsView.OnScrollToRequested (Xamarin.Forms.ScrollToRequestEventArgs e) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Items\ItemsView.cs:219
    at Xamarin.Forms.ItemsView.ScrollTo (System.Int32 index, System.Int32 groupIndex, Xamarin.Forms.ScrollToPosition position, System.Boolean animate) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Items\ItemsView.cs:172
    at Xamarin.Forms.Platform.Android.CarouselViewRenderer.UpdateFromCurrentItem () [0x0003c] in D:\a\1\s\Xamarin.Forms.Platform.Android\CollectionView\CarouselViewRenderer.cs:513
    at Xamarin.Forms.Platform.Android.CarouselViewRenderer.OnElementPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs changedProperty) [0x00079] in D:\a\1\s\Xamarin.Forms.Platform.Android\CollectionView\CarouselViewRenderer.cs:171
    at at (wrapper delegate-invoke) <Module>.invoke_void_object_PropertyChangedEventArgs(object,System.ComponentModel.PropertyChangedEventArgs) in :0
    at Xamarin.Forms.BindableObject.OnPropertyChanged (System.String propertyName) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:266
    at Xamarin.Forms.Element.OnPropertyChanged (System.String propertyName) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Element.cs:362
    at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x00114] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:510
    at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00173] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:446
    at Xamarin.Forms.BindingExpression.ApplyCore (System.Object sourceObject, Xamarin.Forms.BindableObject target, Xamarin.Forms.BindableProperty property, System.Boolean fromTarget) [0x00226] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:160
    at Xamarin.Forms.BindingExpression.Apply (System.Boolean fromTarget) [0x0003e] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:56
    at Xamarin.Forms.BindingExpression+BindingExpressionPart.<PropertyChanged>b__49_0 () [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:762
    at Xamarin.Forms.BindingExpression+BindingExpressionPart.PropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs args) [0x000cb] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:773
    at Xamarin.Forms.BindingExpression+WeakPropertyChangedProxy.OnPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) [0x00012] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:666
    at at (wrapper delegate-invoke) <Module>.invoke_void_object_PropertyChangedEventArgs(object,System.ComponentModel.PropertyChangedEventArgs) in :0
    at CVCurrentItemBug.ViewModels.MainViewModel.RaisePropertyChanged (System.String property) [0x00001] in D:\CVCurrentItemBug\src\CVCurrentItemBug\ViewModels\MainViewModel.cs:49
    at CVCurrentItemBug.ViewModels.MainViewModel.set_SelectedItem (System.String value) [0x0001a] in D:\CVCurrentItemBug\src\CVCurrentItemBug\ViewModels\MainViewModel.cs:35
    at CVCurrentItemBug.ViewModels.MainViewModel.OnClear () [0x00008] in D:\CVCurrentItemBug\src\CVCurrentItemBug\ViewModels\MainViewModel.cs:41
    at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1021
    at Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0 () [0x00000] in <a54fd02a94254b8f91efc9ea3cec736c>:0
    at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <a54fd02a94254b8f91efc9ea3cec736c>:0
    at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in <a54fd02a94254b8f91efc9ea3cec736c>:0
    at at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.1(intptr,intptr) in :0
    at java.lang.IllegalArgumentException: Invalid target position in :0
    at at androidx.recyclerview.widget.RecyclerView$SmoothScroller.start(RecyclerView.java:11807) in :0
    at at androidx.recyclerview.widget.RecyclerView$LayoutManager.startSmoothScroll(RecyclerView.java:8470) in :0
    at at crc64ee486da937c010f4.ButtonRenderer.n_onClick(Native Method) in :0
    at at crc64ee486da937c010f4.ButtonRenderer.onClick(ButtonRenderer.java:104) in :0
    at at android.view.View.performClick(View.java:6597) in :0
    at at android.view.View.performClickInternal(View.java:6574) in :0
    at at android.view.View.access$3100(View.java:778) in :0
    at at android.view.View$PerformClick.run(View.java:25885) in :0
    at at android.os.Handler.handleCallback(Handler.java:873) in :0
    at at android.os.Handler.dispatchMessage(Handler.java:99) in :0
    at at android.os.Looper.loop(Looper.java:193) in :0
    at at android.app.ActivityThread.main(ActivityThread.java:6669) in :0
    at at java.lang.reflect.Method.invoke(Native Method) in :0
    at at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) in :0
    at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) in :0

Reproduction Link

I have created an updated sample project for anyone to take a look at. This uses XF 5.0.0.1874 and the app does crash on clearing out the CarouselView

Versions

Xamarin.Forms 5.0.0.1874

nionzion commented 3 years ago

Workaround

[assembly: ExportRenderer(typeof(CarouselView), typeof(CustomCarouselViewRenderer))]
namespace ...
public class CustomCarouselViewRenderer : CarouselViewRenderer
{
        public CustomCarouselViewRenderer(Context context) : base(context)
        {
        }

        protected override void ScrollTo(ScrollToRequestEventArgs args)
        {
            if(args.Item != null)
                base.ScrollTo(args);
        }
}

To obtain custom functionality you can customize the ScrollTo-Method

SkyeHoefling commented 3 years ago

@nionzion Thanks for the workaround, this does prevent the application from crashing and it is something I was looking at for a potential fix to XF. I believe the problem to be more complex than that as ScrollTo stopped working after my first time clearing out the list and setting the CurrentItem = null.

It appears that there is some state management code that happens in the parent class that needs to be reset. Otherwise setting the CurrentItem won't invoke the scroll event.

CocoCR300 commented 3 years ago

This also affects UWP (https://github.com/xamarin/Xamarin.Forms/issues/13133#issuecomment-762308838):

  1. Binding an empty collection to the ItemsSource property causes CurrentItem being set to null and then the app crashes.
  2. In a collection with some items, clearing it and then scrolling causes the app to crash. The last element displayed remains there.

Testing Android 10 - API 29 didn't throw me any errors. All using X.F 5.0.0.1874

rmarinho commented 3 years ago

Hey i can fix the issue of the crash but i m trying to figure out why it doesn't work the 2nd time (clear the items) .

Will keep you posted @ahoefling

working on it here https://github.com/xamarin/Xamarin.Forms/pull/13536

KasperDanielsen commented 2 years ago

@nionzion solution also worked for me on IOS. Thank you. Spent days on trying to fix this.