Bug 47911 - System.NullReferenceException when you call Platform.SetRenderer (Android)
Summary: System.NullReferenceException when you call Platform.SetRenderer (Android)
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: 2.3.3
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-11-24 21:26 UTC by Alex Rainman
Modified: 2017-05-31 09:08 UTC (History)
3 users (show)

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

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 FIXED

Description Alex Rainman 2016-11-24 21:26:43 UTC
I have this piece of code to convert Forms view to native view in Android:

public static ViewGroup ConvertFormsToNative(Xamarin.Forms.View view, Rectangle size)
{
    if (Platform.GetRenderer(view) == null)
	Platform.SetRenderer(view, Platform.CreateRenderer(view));
    var vRenderer = Platform.GetRenderer(view);
            
    var viewGroup = vRenderer.ViewGroup;
    vRenderer.Tracker.UpdateLayout ();
    var layoutParams = new ViewGroup.LayoutParams ((int)size.Width, (int)size.Height);
    viewGroup.LayoutParameters = layoutParams;
    view.Layout (size);
    viewGroup.Layout (0, 0, (int)view.WidthRequest, (int)view.HeightRequest);
    return viewGroup; 
}

Everything works find, but if the Forms view includes a ListView, i get System.NullReferenceException (Object reference not set to an instance of an object.)

This happens with any Forms version > 2.3.0.107. I wonder what changed in ListViewAdapater.cs?

StackTrace:

  at Xamarin.Forms.Platform.Android.ListViewAdapter..ctor (Android.Content.Context context, Android.Widget.ListView realListView, Xamarin.Forms.ListView listView) [0x00096] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Renderers\ListViewAdapter.cs:56 
  at Xamarin.Forms.Platform.Android.ListViewRenderer.OnElementChanged (Xamarin.Forms.Platform.Android.ElementChangedEventArgs`1[TElement] e) [0x00100] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Renderers\ListViewRenderer.cs:142 
  at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetElement (TElement element) [0x000f4] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:193 
  at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00027] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:129 
  at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) [0x0001f] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Platform.cs:289 
  at Xamarin.Forms.Platform.Android.VisualElementPackager.AddChild (Xamarin.Forms.VisualElement view, Xamarin.Forms.Platform.Android.IVisualElementRenderer oldRenderer, Xamarin.Forms.Platform.Android.RendererPool pool, System.Boolean sameChildren) [0x00023] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:74 
  at Xamarin.Forms.Platform.Android.VisualElementPackager.SetElement (Xamarin.Forms.VisualElement oldElement, Xamarin.Forms.VisualElement newElement) [0x00104] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:207 
  at Xamarin.Forms.Platform.Android.VisualElementPackager.Load () [0x00000] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementPackager.cs:58 
  at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetPackager (Xamarin.Forms.Platform.Android.VisualElementPackager packager) [0x00007] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:337 
  at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].SetElement (TElement element) [0x00111] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:196 
  at Xamarin.Forms.Platform.Android.VisualElementRenderer`1[TElement].Xamarin.Forms.Platform.Android.IVisualElementRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x00027] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\VisualElementRenderer.cs:129 
  at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element) [0x0001f] in C:\BuildAgent2\work\ca3766cfc22354a1\Xamarin.Forms.Platform.Android\Platform.cs:289 
  at CarouselView.FormsPlugin.Android.FormsToNativeDroid.ConvertFormsToNative (Xamarin.Forms.View view, Xamarin.Forms.Rectangle size) [0x0000e] in /Users/alexrainman/Desktop/GitHub/CarouselView/CarouselView/CarouselView.FormsPlugin.Android/FormsToNativeDroid.cs:15 
  at CarouselView.FormsPlugin.Android.CarouselViewRenderer+PageAdapter.InstantiateItem (Android.Views.ViewGroup container, System.Int32 position) [0x000ab] in /Users/alexrainman/Desktop/GitHub/CarouselView/CarouselView/CarouselView.FormsPlugin.Android/CarouselViewImplementation.cs:279 
  at Android.Support.V4.View.PagerAdapter.n_InstantiateItem_Landroid_view_ViewGroup_I (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_container, System.Int32 position) [0x00011] in <27c17fe440cf491ba8255bcefade6e02>:0 
  at (wrapper dynamic-method) System.Object:fadbef30-65cf-4c1c-9bfa-e56e954786c9 (intptr,intptr,intptr,int)
Comment 1 adrianknight89 2016-11-25 03:03:55 UTC
Can you post a minimal repro? How are you calling ConvertFormsToNative? I suspect that the Platform is somehow null.
Comment 2 Alex Rainman 2016-11-25 17:26:19 UTC
Platform is not null. If you take a look at StackTrace, the problem is at ListViewAdapter ctor and that's core level so i cannot handle it. Please download this repository and run Demo/Droid: https://github.com/alexrainman/CarouselView/tree/master/CarouselView

I use ConvertFormsToNative at InstantiateItem of ViewPager Adapater.
Comment 3 adrianknight89 2016-11-26 20:43:42 UTC
Sorry, if I wasn't clear. Recently, code was added to that constructor that uses ListView's Platform to subscribe a message. I ran your code and verified that its Platform is returning null, hence the exception. It looks like MySecondView has no Parent set. If you set its Parent, I assume it should inherit the parent's Platform and pass it down to its children (ListView for example). 

I'm not sure if this is a fix on your end, however. I haven't looked at your CarouselView implementation, but see if you're forgetting to set Parent somewhere. I could see that the CarouselView has its Parent set correctly.
Comment 4 Alex Rainman 2016-11-26 21:07:10 UTC
Excelent, setting the Forms view parent to Element, fixed the issue. Thanks.
Comment 5 adrianknight89 2016-11-26 21:10:50 UTC
Sweet. You could close this bug then.