Bug 43824 - System.NullReferenceException at Xamarin.Forms.Platform.Android.ViewRenderer`2[TView,TNativeView].GetDesiredSize (Int32 widthConstraint, Int32 heightConstraint)
Summary: System.NullReferenceException at Xamarin.Forms.Platform.Android.ViewRenderer`...
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Android ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-08-29 09:53 UTC by Mathias Hartner
Modified: 2016-09-30 18:42 UTC (History)
2 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 Mathias Hartner 2016-08-29 09:53:23 UTC
Hi Xamarin team,

I encountered following problem:

I have my own view with its own view renderer. If I add it as content of a ContentPage, everything works just fine when I show the page on my phone via "Navigation.PushAsync()".
But if I add it as children to a StackLayout, assign that StackLayout as content of a ContentPage and then try to show that page on my phone, I get following error:

An unhandled exception occurred and was caught by global exception handler AndroidEnvironmentOnUnhandledExceptionRaiser System.NullReferenceException: Object reference not set to an instance of an object.
  at Xamarin.Forms.Platform.Android.ViewRenderer`2[TView,TNativeView].GetDesiredSize (Int32 widthConstraint, Int32 heightConstraint) [0x0001c] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\ViewRenderer.cs:63 
  at *ProjectName*.Mobile.Droid.Views.Renderers.CustomViewRendererAndroid.GetDesiredSize (Int32 widthConstraint, Int32 heightConstraint) [0x00001] in D:\Sources\KSBCollection\Branches\Mobile\Mobile\KSB.Mobile.Android\Views\Renderers\OperationPointViewRendererAndroid.cs:51 
  at Xamarin.Forms.Platform.Android.Platform.Xamarin.Forms.IPlatform.GetNativeSize (Xamarin.Forms.VisualElement view, Double widthConstraint, Double heightConstraint) [0x0008e] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\Platform.cs:1029 
  at Xamarin.Forms.VisualElement.OnSizeRequest (Double widthConstraint, Double heightConstraint) [0x0002d] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\VisualElement.cs:608 
  at Xamarin.Forms.VisualElement.OnMeasure (Double widthConstraint, Double heightConstraint) [0x00000] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\VisualElement.cs:580 
  at Xamarin.Forms.VisualElement.GetSizeRequest (Double widthConstraint, Double heightConstraint) [0x00053] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\VisualElement.cs:467 
  at Xamarin.Forms.VisualElement.Measure (Double widthConstraint, Double heightConstraint, MeasureFlags flags) [0x00000] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\VisualElement.cs:514 
  at Xamarin.Forms.StackLayout.CalculateNaiveLayout (Xamarin.Forms.LayoutInformation layout, StackOrientation orientation, Double x, Double y, Double widthConstraint, Double heightConstraint) [0x000a8] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\StackLayout.cs:149 
  at Xamarin.Forms.StackLayout.CalculateLayout (Xamarin.Forms.LayoutInformation layout, Double x, Double y, Double widthConstraint, Double heightConstraint, Boolean processExpanders) [0x00058] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\StackLayout.cs:109 
  at Xamarin.Forms.StackLayout.LayoutChildren (Double x, Double y, Double width, Double height) [0x0005b] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\StackLayout.cs:43 
  at Xamarin.Forms.Layout.UpdateChildrenLayout () [0x000c7] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\Layout.cs:228 
  at Xamarin.Forms.Layout.OnSizeAllocated (Double width, Double height) [0x0000f] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\Layout.cs:193 
  at Xamarin.Forms.VisualElement.SizeAllocated (Double width, Double height) [0x00000] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\VisualElement.cs:613 
  at Xamarin.Forms.VisualElement.SetSize (Double width, Double height) [0x00021] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\VisualElement.cs:752 
  at Xamarin.Forms.VisualElement.set_Bounds (Rectangle value) [0x0005d] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\VisualElement.cs:156 
  at Xamarin.Forms.VisualElement.Layout (Rectangle bounds) [0x00000] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\VisualElement.cs:509 
  at Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion (Xamarin.Forms.VisualElement child, Rectangle region) [0x0016b] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\Layout.cs:147 
  at Xamarin.Forms.Page.LayoutChildren (Double x, Double y, Double width, Double height) [0x00107] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\Page.cs:177 
  at Xamarin.Forms.Page.UpdateChildrenLayout () [0x000c6] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\Page.cs:250 
  at Xamarin.Forms.Page.OnSizeAllocated (Double width, Double height) [0x0000f] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\Page.cs:231 
  at Xamarin.Forms.VisualElement.SizeAllocated (Double width, Double height) [0x00000] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\VisualElement.cs:613 
  at Xamarin.Forms.VisualElement.SetSize (Double width, Double height) [0x00021] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\VisualElement.cs:752 
  at Xamarin.Forms.VisualElement.set_Bounds (Rectangle value) [0x0005d] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\VisualElement.cs:156 
  at Xamarin.Forms.VisualElement.Layout (Rectangle bounds) [0x00000] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\VisualElement.cs:509 
  at Xamarin.Forms.Layout.LayoutChildIntoBoundingRegion (Xamarin.Forms.VisualElement child, Rectangle region) [0x0000a] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\Layout.cs:119 
  at Xamarin.Forms.Page.LayoutChildren (Double x, Double y, Double width, Double height) [0x00107] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\Page.cs:177 
  at Xamarin.Forms.Page.UpdateChildrenLayout () [0x000c6] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\Page.cs:250 
  at Xamarin.Forms.Page.OnSizeAllocated (Double width, Double height) [0x0000f] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\Page.cs:231 
  at Xamarin.Forms.VisualElement.SizeAllocated (Double width, Double height) [0x00000] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\VisualElement.cs:613 
  at Xamarin.Forms.Page.OnChildMeasureInvalidated (Xamarin.Forms.VisualElement child, InvalidationTrigger trigger) [0x000a9] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\Page.cs:289 
  at Xamarin.Forms.Page.OnChildMeasureInvalidated (System.Object sender, System.EventArgs e) [0x00013] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\Page.cs:213 
  at Xamarin.Forms.VisualElement.InvalidateMeasure (InvalidationTrigger trigger) [0x0000b] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\VisualElement.cs:638 
  at Xamarin.Forms.VisualElement.set_IsPlatformEnabled (Boolean value) [0x0001c] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\VisualElement.cs:369 
  at Xamarin.Forms.Platform.Android.Platform+<>c.<.cctor>b__107_0 (Xamarin.Forms.BindableObject bindable, System.Object oldvalue, System.Object newvalue) [0x0000a] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\Platform.cs:30 
  at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindablePropertyContext context, System.Object value, Boolean currentlyApplying, SetValueFlags attributes, Boolean silent) [0x00108] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:584 
  at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes) [0x0014b] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:378 
  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, Boolean fromStyle, Boolean checkAccess) [0x0005f] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:531 
  at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Core\BindableObject.cs:83 
  at Xamarin.Forms.Platform.Android.Platform.SetRenderer (Xamarin.Forms.VisualElement bindable, IVisualElementRenderer value) [0x00000] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\Platform.cs:294 
  at Xamarin.Forms.Platform.Android.CarouselPageAdapter.InstantiateItem (Android.Views.ViewGroup container, Int32 position) [0x0001a] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\Renderers\CarouselPageAdapter.cs:87 
  at Android.Support.V4.View.PagerAdapter.n_InstantiateItem_Landroid_view_ViewGroup_I (IntPtr jnienv, IntPtr native__this, IntPtr native_container, Int32 position) [0x00011] in <filename unknown>:0 
  at (wrapper dynamic-method) System.Object:355eddc3-1f7a-4c7c-aa15-dd1ca6a2efee (intptr,intptr,intptr,int)

The problem occurs in "GetDesiredSize()". So I was setting a breakpoint and realized that the "heightConstraint" was always zero but the "widthConstraint" was a huge negative integer.
I came up with a workaround to overcome this. You need to override "GetDesiredSize()" in your CustomViewRenderer like so:

/// <summary>
/// Gets the desired size for the control.
/// </summary>
/// <param name="widthConstraint"></param>
/// <param name="heightConstraint"></param>
/// <returns></returns>
public override SizeRequest GetDesiredSize(int widthConstraint, int heightConstraint)
{
    return new SizeRequest { Request = new Size(0, 0) };
}
Comment 1 Samantha Houts [MSFT] 2016-09-10 00:35:31 UTC
Thank you for taking the time to submit this report. After reviewing the description of this bug, we believe it no longer affects the current version of Xamarin.Forms. If you are still experiencing the issue after updating your packages, please reopen this report with an attached reproduction. 

For your convenience, we have created some reproduction best practices viewable here: https://gist.github.com/jassmith/92405c300e54a01dcc6d  

Warm regards, 
Xamarin Forms Team
Comment 2 Samantha Houts [MSFT] 2016-09-30 18:42:43 UTC
Setting status to closed per Comment 1.