Bug 51648 - InvalidCastException when listview item is tapped under RecycleElement strategy
Summary: InvalidCastException when listview item is tapped under RecycleElement strategy
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: 2.3.3
Hardware: PC Mac OS
: Normal normal
Target Milestone: ---
Assignee: Paul DiPietro [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2017-01-19 19:39 UTC by Pete Schmitz
Modified: 2018-02-12 18:30 UTC (History)
10 users (show)

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


Attachments
Repro solution (70.66 KB, application/zip)
2017-11-28 16:48 UTC, Tom Gilder
Details


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:
Status:
RESOLVED ANSWERED

Description Pete Schmitz 2017-01-19 19:39:46 UTC
We have users that are experiencing the following exception when they tap on an item in a listview that is set to RecycleElement strategy:

System.InvalidCastExceptionSpecified cast is not valid.
Raw
Xamarin.Forms.Platform.Android.ListViewAdapter.HandleItemClick(AdapterView parent, View view, int position, long id)<eed8aa96500949579ca8d725b7c74dd0>:0
Xamarin.Forms.Platform.Android.CellAdapter.OnItemClick(AdapterView parent, View view, int position, long id)<eed8aa96500949579ca8d725b7c74dd0>:0
Android.Widget.AdapterView.IOnItemClickListenerInvoker.n_OnItemClick_Landroid_widget_AdapterView_Landroid_view_View_IJ(IntPtr jnienv, IntPtr native__this, IntPtr native_parent, IntPtr native_view, int position, long id)<f702ac4a2a794421adc5078956c64f18>:0
at (wrapper dynamic-method) System.Object:3c863897-ab0e-4d23-90bc-074d65d8cc28 (intptr,intptr,intptr,intptr,int,long)

This appears to be identical to the issue raised in this thread, but no clear resolution is mentioned - https://forums.xamarin.com/discussion/82273/how-can-i-use-recycleelement-with-custom-renderers-in-a-listview

In our case, user events indicate that the listview appears to initially work, a context/itemssource change occurs, and then the user will encounter the above exception when then they interact once more with the listview.

- Our custom cells are inheriting from XLab's ExtendedViewCell (2.2.0-pre5)

- Forms 2.3.3.175

Xamarin.Android Version: 7.0.2.42 (Xamarin Studio Community)
SDK Tools Version: 25.2.5
SDK Platform Tools Version: 25.0.3
SDK Build Tools Version: 24.0.1
java version "1.8.0_111"
Comment 1 Night Owl 2017-01-25 19:49:48 UTC
We have a similar issue on Xamarin 7.1.0.13 and forms 2.3.3.168.
But we do not use custom cells.

All out listview cell templates are defined in the same XAML file as the list, using regular forms controls.
Comment 2 Paul DiPietro [MSFT] 2017-02-15 16:15:01 UTC
Could you upload a reproduction, potentially without the XLabs ViewCell to help simplify things? Does this occur on multiple devices/emulators?
Comment 3 Paul DiPietro [MSFT] 2017-06-19 19:12:35 UTC
Because we have not received a reply to our request for more information we are closing this issue. If you are still encountering this issue, please reopen the ticket and attach a project that demonstrates the issue.
Comment 4 Emman Vazz 2017-09-10 05:31:31 UTC
We have also seen this crash with 2.4.0.269-pre2 on a Galaxy S5 device. We don't use XLab ViewCell. Will try to create a reproduction project.
Comment 5 Emman Vazz 2017-09-10 05:33:45 UTC
The s5 is running 6.0.1
Comment 6 Emman Vazz 2017-09-10 05:46:51 UTC
Here is the stacktrace 
[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] System.InvalidCastException: Specified cast is not valid.
[MonoDroid]   at Xamarin.Forms.Platform.Android.ListViewAdapter.HandleItemClick (Android.Widget.AdapterView parent, Android.Views.View view, System.Int32 position, System.Int64 id) [0x00024] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Renderers\ListViewAdapter.cs:384 
[MonoDroid]   at Xamarin.Forms.Platform.Android.CellAdapter.OnItemClick (Android.Widget.AdapterView parent, Android.Views.View view, System.Int32 position, System.Int64 id) [0x0002e] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\CellAdapter.cs:144 
[MonoDroid]   at Android.Widget.AdapterView+IOnItemClickListenerInvoker.n_OnItemClick_Landroid_widget_AdapterView_Landroid_view_View_IJ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_parent, System.IntPtr native_view, System.Int32 position, System.Int64 id) [0x00017] in <d855bac285f44dda8a0d8510b679b1e2>:0 
[MonoDroid]   at (wrapper dynamic-method) System.Object:843f8ff0-7cab-47e2-b602-bc92032cb04d (intptr,intptr,intptr,intptr,int,long)
Thread started: <Thread Pool> #33
[Mono] The request to load the assembly System.Core v4.0.0.0 was remapped to v2.0.5.0
[Mono] The request to load the assembly System.Core v4.0.0.0 was remapped to v2.0.5.0
[MobileCenterCrashes] Unhandled Exception:
[MobileCenterCrashes] System.InvalidCastException: Specified cast is not valid.
[MobileCenterCrashes]   at Xamarin.Forms.Platform.Android.ListViewAdapter.HandleItemClick (Android.Widget.AdapterView parent, Android.Views.View view, System.Int32 position, System.Int64 id) [0x00024] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Renderers\ListViewAdapter.cs:384 
[MobileCenterCrashes]   at Xamarin.Forms.Platform.Android.CellAdapter.OnItemClick (Android.Widget.AdapterView parent, Android.Views.View view, System.Int32 position, System.Int64 id) [0x0002e] in C:\BuildAgent3\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\CellAdapter.cs:144 
[MobileCenterCrashes]   at Android.Widget.AdapterView+IOnItemClickListenerInvoker.n_OnItemClick_Landroid_widget_AdapterView_Landroid_view_View_IJ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_parent, System.IntPtr native_view, System.Int32 position, System.Int64 id) [0x00017] in <d855bac285f44dda8a0d8510b679b1e2>:0 
[MobileCenterCrashes]   at (wrapper dynamic-method) System.Object:843f8ff0-7cab-47e2-b602-bc92032cb04d (intptr,intptr,intptr,intptr,int,long)
[libc] Fatal signal 6 (SIGABRT) at 0x00005ec8 (code=-6), thread 24264 (lopmentTemplate)
Comment 7 Mikalai Daronin 2017-09-19 06:32:32 UTC
Same problem without XLabs ViewCell.
Comment 8 Emman Vazz 2017-09-19 21:11:45 UTC
I fixed my issue. My issue was clicking on a row that was getting clear when the listview was refreshing.
Comment 9 Tom Gilder 2017-11-28 16:48:32 UTC
Created attachment 25851 [details]
Repro solution

Please reopen this. Simple reproduction attached. 

1. Run the attached project on Android (tested on Android 6.0 in the simulator)
2. Tap the list header
3. Boom - InvalidCastException


Note that:

1. The ListView must be a subclass
2. Its ListViewCachingStrategy must be RecycleElement or RecycleElementAndDataTemplate
3. Items must be added to the ListView after the page has loaded
Comment 10 Tom Gilder 2017-11-28 16:52:29 UTC
Correction: the ListView doesn't have to be a subclass.

The crash also occurs with <ListView CachingStrategy="RecycleElement">.
Comment 11 Ricardo K. 2017-12-23 10:45:56 UTC
Same problem using DLToolkit.Form FlowListView

If I tap on the header the app is crashing

here the project:

https://github.com/Integreat/app-cross_platform

here the code part:

https://github.com/Integreat/app-cross_platform/blob/dev/Integreat/Integreat.Shared/Pages/ExtrasContentPage.xaml
Comment 12 Mikalai Daronin 2018-01-04 10:37:27 UTC
Why is it marked as RESOLVED NORESPONSE?
Comment 13 Ricardo K. 2018-01-13 13:17:11 UTC
still open?
Comment 14 Tom Gilder 2018-01-17 20:50:36 UTC
I've opened a new issue for this on GitHub: https://github.com/xamarin/Xamarin.Forms/issues/1598