Bug 57317 - Modifying Cell.ContextActions can crash on Android
Summary: Modifying Cell.ContextActions can crash on Android
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: 2.3.4
Hardware: All All
: --- normal
Target Milestone: ---
Assignee: Jimmy [MSFT]
: 59194 ()
Depends on:
Reported: 2017-06-09 13:33 UTC by Alexander Hardwicke
Modified: 2017-09-05 17:10 UTC (History)
4 users (show)

Tags: ac android cell contextactions
Is this bug a regression?: ---
Last known good build:

Example project (50.75 KB, application/zip)
2017-06-09 13:33 UTC, Alexander Hardwicke

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Alexander Hardwicke 2017-06-09 13:33:52 UTC
Created attachment 22810 [details]
Example project

Modifying Cell.ContextActions from a command or click event of a MenuItem that is inside the same ContextActions collection will cause a NullReferenceException on Android if FormsAppCompatActivity is used.

Steps to Reproduce
 1) Ensure that the Android application is using FormsAppCompatActivity.
 2) Have a Cell with at least one ContextAction.
 3) Set the ContextAction’s command/click event to remove one or more ContextActions from the cell it's contained in (e.g. remove itself).

That is, something along the lines of
var cell = new TextCell();
var menuItem = new MenuItem();
menuItem.Command = new Command(() => cell.ContextActions.Remove(menuItem));

Actual Results
 A NullReferenceException is thrown and the application crashes.

Expected Results
 The ContextAction(s) will be successfully removed.

Additional Information
 The problem seems to be that we’re getting a SupportActionMode in CellAdapter.cs because we’re using FormsAppCompatActivity, but in CellAdapter::OnContextItemsChanged, there’s a direct call to _actionMode.Invalidate() with no null checking (and no invalidation of support).

 I’ve made a fix and written a UI test for it and I’ll make a pull request once this bug is submitted.

 I've also attached a project that can reproduce the problem.
Comment 1 Jimmy [MSFT] 2017-06-09 20:26:03 UTC
Thank you for filing this report! I was able to reproduce this issue with the project attached using Forms and 2.3.5-pre3. The app crashes with the following stack trace:

> 06-09 13:14:29.632 I/MonoDroid(17598): System.NullReferenceException: Object reference not set to an instance of an object.
> 06-09 13:14:29.632 I/MonoDroid(17598):   at Xamarin.Forms.Platform.Android.CellAdapter.OnContextItemsChanged (System.Object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x00007] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\CellAdapter.cs:296 
> 06-09 13:14:29.632 I/MonoDroid(17598):   at (wrapper delegate-invoke) <Module>:invoke_void_object_NotifyCollectionChangedEventArgs (object,System.Collections.Specialized.NotifyCollectionChangedEventArgs)
> 06-09 13:14:29.632 I/MonoDroid(17598):   at System.Collections.ObjectModel.ObservableCollection`1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedEventArgs e) [0x0000f] in <a547bd0d78184f26ab08d022f013c1e1>:0 
> 06-09 13:14:29.632 I/MonoDroid(17598):   at System.Collections.ObjectModel.ObservableCollection`1[T].OnCollectionChanged (System.Collections.Specialized.NotifyCollectionChangedAction action, System.Object item, System.Int32 index) [0x00009] in <a547bd0d78184f26ab08d022f013c1e1>:0 
> 06-09 13:14:29.632 I/MonoDroid(17598):   at System.Collections.ObjectModel.ObservableCollection`1[T].RemoveItem (System.Int32 index) [0x0002b] in <a547bd0d78184f26ab08d022f013c1e1>:0 
> 06-09 13:14:29.632 I/MonoDroid(17598):   at System.Collections.ObjectModel.Collection`1[T].Remove (T item) [0x00027] in <3fd174ff54b146228c505f23cf75ce71>:0 
> 06-09 13:14:29.632 I/MonoDroid(17598):   at Xamarin.Forms.ContextActionsBug.MainPage.MenuItem_Clicked (System.Object sender, System.EventArgs e) [0x00001] in C:\Users\jigarrid\Documents\_Bugs\57317 cell actions\repro\Xamarin.Forms.ContextActionsBug\Xamarin.Forms.ContextActionsBug\MainPage.xaml.cs:19 
> 06-09 13:14:29.632 I/MonoDroid(17598):   at Xamarin.Forms.MenuItem.OnClicked () [0x0000a] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\MenuItem.cs:85 
> 06-09 13:14:29.632 I/MonoDroid(17598):   at Xamarin.Forms.MenuItem.Xamarin.Forms.IMenuItemController.Activate () [0x00021] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Core\MenuItem.cs:96 
> 06-09 13:14:29.632 I/MonoDroid(17598):   at Xamarin.Forms.Platform.Android.CellAdapter.OnActionItemClickedImpl (Android.Views.IMenuItem item) [0x00018] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\CellAdapter.cs:260 
> 06-09 13:14:29.633 I/MonoDroid(17598):   at Xamarin.Forms.Platform.Android.CellAdapter.global::Android.Support.V7.View.ActionMode.ICallback.OnActionItemClicked (Android.Support.V7.View.ActionMode mode, Android.Views.IMenuItem item) [0x00000] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\CellAdapter.cs:91 
> 06-09 13:14:29.633 I/MonoDroid(17598):   at Android.Support.V7.View.ActionMode+ICallbackInvoker.n_OnActionItemClicked_Landroid_support_v7_view_ActionMode_Landroid_view_MenuItem_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_mode, System.IntPtr native_item) [0x00019] in <c690702816034f28aca512af1f0abd66>:0 
> 06-09 13:14:29.633 I/MonoDroid(17598):   at (wrapper dynamic-method) System.Object:09c12ae3-2cad-499f-854a-bf057167109e (intptr,intptr,intptr,intptr)

The pull request for a fix appears to be: https://github.com/xamarin/Xamarin.Forms/pull/982
Comment 2 Samantha Houts [MSFT] 2017-08-23 19:37:26 UTC
Should be fixed in 2.4.0-pre1. Thank you!
Comment 3 Jimmy [MSFT] 2017-09-05 17:10:44 UTC
*** Bug 59194 has been marked as a duplicate of this bug. ***