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

[Bug] InnerGestureListener IOnGestureListener.OnLongPress NullReferenceException #15709

Open schacherlj opened 1 year ago

schacherlj commented 1 year ago

Description

DragGestureRecognizer is disposed before long press is processed

System.NullReferenceException: Object reference not set to an instance of an object. IOnGestureListener.OnLongPress (Android.Views.MotionEvent e) D:\a\1\s\Xamarin.Forms.Platform.Android\InnerGestureListener.cs:120

Steps to Reproduce

  1. create content page with telerik RadTabView with more tabs
  2. place CollectionView on first tab
  3. use grid with DragGestureRecognizer as item template
  4. drag item horizontally to switch tabs
  5. second tab is displayed
  6. app crashes

Basic Information

Environment

Show/Hide Visual Studio info ``` Microsoft Visual Studio Professional 2022 Version 17.5.1 VisualStudio.17.Release/17.5.1+33424.131 Microsoft .NET Framework Version 4.8.04084 Installed Version: Professional Visual C++ 2022 00483-10700-27530-AA705 Microsoft Visual C++ 2022 C# Tools 4.5.0-6.23123.11+271ccd71554f7d28d2f90551aafd0bdeb5d327aa C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used. Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines Mono Debugging for Visual Studio 17.5.9 (11975e6) Support for debugging Mono processes with Visual Studio. NuGet Package Manager 6.5.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/ Open Command Line 2.5.238 2.5.238 Visual C++ for Cross Platform Mobile Development (Android) 17.0.33312.129 Visual C++ for Cross Platform Mobile Development (Android) Xamarin 17.5.0.173 (d17-5@33e727c) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android. Xamarin Designer 17.5.3.46 (remotes/origin/d17-5@e4dd80b2bb) Visual Studio extension to enable Xamarin Designer tools in Visual Studio. Xamarin Templates 17.5.41 (ba80d05) Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms. Xamarin.Android SDK 13.2.0.0 (d17-5/797e2e1) Xamarin.Android Reference Assemblies and MSBuild support. Mono: 6dd9def Java.Interop: xamarin/java.interop/main@149d70fe SQLite: xamarin/sqlite/3.40.0@fdc1e34 Xamarin.Android Tools: xamarin/xamarin-android-tools/main@9f02d77 Xamarin.iOS and Xamarin.Mac SDK 16.2.0.5 (7738c90c9) Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support. ```

Build Logs

Screenshots

Reproduction Link

Workaround

kobbok commented 8 months ago

I'm experiencing the same problem, for me it's happening with a CollectionView when scrolling it up and down repeatedly.

It seems the InnerGestureListener gets disposed, then the internal InnerGestureListener constructor gets called, judging from the comment above that function the gesture listener is still required by the App but since it was disposed the _dragAndDropGestureListener is now null causing the NullReferenceException.

It looks like the void GestureDetector.IOnGestureListener.OnLongPress(MotionEvent e) function in Xamarin.Forms.Platform.Android\InnerGestureListener.cs is missing a disposed check. Something like the following should fix the problem:

 void GestureDetector.IOnGestureListener.OnLongPress(MotionEvent e)
 {
+    if (_disposed)
+        return;
+
     SetStartingPosition(e);
     _dragAndDropGestureHandler.OnLongPress(e);
 }

Edit: Learned that CollectionView and DragGestureRecognizer and DropGestureRecognizer are way harder to combine than seems at first glance. Fixed my problem by using https://github.com/billvenhaus/ReorderableCollectionView.Forms amazing package Sadly this probably doesn't fix the problem for OP