Bug 38989 - [Android] NullReferenceException when using a custom ViewCellRenderer
Summary: [Android] NullReferenceException when using a custom ViewCellRenderer
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: 2.0.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2016-02-22 14:43 UTC by Michael Rumpler
Modified: 2017-02-01 18:10 UTC (History)
6 users (show)

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

repro project (44.05 KB, application/x-zip-compressed)
2016-02-22 14:43 UTC, Michael Rumpler

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 Michael Rumpler 2016-02-22 14:43:30 UTC
Created attachment 15124 [details]
repro project

When you create a custom ViewCell which adds some elements to its View and that custom ViewCell uses a custom renderer in the Android project, then there will be a NullReferenceException after the custom renderers GetCellCore method successfully returned the cell.

The exception is raised here:

  at Xamarin.Forms.Platform.Android.ConditionalFocusLayout.ApplyTouchListenersToSpecialCells (Xamarin.Forms.Cell item) [0x0008f] in <filename unknown>:0 
  at Xamarin.Forms.Platform.Android.TableViewModelRenderer.GetView (Int32 position, Android.Views.View convertView, Android.Views.ViewGroup parent) [0x00153] in <filename unknown>:0 
  at Android.Widget.BaseAdapter.n_GetView_ILandroid_view_View_Landroid_view_ViewGroup_ (IntPtr jnienv, IntPtr native__this, Int32 position, IntPtr native_convertView, IntPtr native_parent) [0x0001a] in /Users/builder/data/lanes/2692/e98e9627/source/monodroid/src/Mono.Android/platforms/android-23/src/generated/Android.Widget.BaseAdapter.cs:509 
  at (wrapper dynamic-method) System.Object:8ed84031-e813-4601-b703-371118af0f60 (intptr,intptr,int,intptr,intptr)

The bug can be reproduced with the attached project which uses XF 2.1.0-pre4.

You can work around this bug when you do not assign anything to the custom ViewCells View property or do not call InitializeComponent if you defined it in xaml.

This bug is a duplicate of bug #29210. That bug has been created on 2015-04-17 for XF 1.4.1 and set to resolved fixed by Rui Marinho on 2015-06-03. But as several others pointed out afterwards, the bug is not fixed. I asked Rui and Anubhav Ranjan (who created it) to reopen 29210, but they did not respond. So I created a duplicate where I can control the status.
Comment 1 toSilence 2016-03-10 13:49:22 UTC
We have the same issiue - looks like the a null comparison is missing in that method. 
It should be easy and fast to fix and we really need this. 
Or a workaround.
Comment 2 rwagh 2016-03-21 10:29:06 UTC
i tried workaround mentioned at http://forums.xamarin.com/discussion/comment/110003/#Comment_110003
and it worked for me

"I resolved the issue by changing my ViewCells to derive from Cell and use CellRenderer. Of course, this requires all of your UI code to be in platform specific projects. Mine already was so it wasn't a big issue for me. If you're relying on Xam Forms for your layout then this approach would not work."
Comment 3 adrianknight89 2016-11-27 21:44:58 UTC
See https://github.com/xamarin/Xamarin.Forms/pull/567
Comment 4 Parmendra Kumar 2017-02-01 18:10:28 UTC
I have checked this issue with Xamarin.Forms and observed that this issue has been fixed.

Hence closing this issue.