Android Custom MasterDetailRenderer null reference

I'm having a NullReferenceException using a custom MasterDetailRenderer for Android, for dynamically controlling the accent color.
This it the renderer code:
public class CustomMasterDetailRenderer : MasterDetailRenderer { protected override void OnAttachedToWindow() { var androidGlow = Android.Graphics.Color.Blue; //just for test SetScrimColor(androidGlow); base.OnAttachedToWindow(); } }

The style being used is MainTheme on this:

<style name="MainTheme" parent="MainTheme.Base">
  </style>
  <!-- Base theme applied no matter what API -->
  <style name="MainTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
    <item name="windowNoTitle">true</item>
    <!--We will be using the toolbar so no need to show ActionBar-->
    <item name="windowActionBar">false</item>
    <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette -->
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">#2196F3</item>
    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">#1976D2</item>
    <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
    <item name="colorAccent">#000000</item>
    <!-- You can also set colorControlNormal, colorControlActivated
         colorControlHighlight and colorSwitchThumbNormal. -->
    <item name="windowActionModeOverlay">true</item>

    <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
  </style>

And this is the exception stacktrace:
11-24 14:30:33.319 I/MonoDroid( 4272): System.NullReferenceException: Object reference not set to an instance of an object. 11-24 14:30:33.319 I/MonoDroid( 4272): at Xamarin.Forms.Platform.Android.MasterDetailRenderer.SetElement (Xamarin.Forms.VisualElement element) [0x0008f] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\Renderers\MasterDetailRenderer.cs:99 11-24 14:30:33.319 I/MonoDroid( 4272): at Xamarin.Forms.Platform.Android.Platform.CreateRenderer (Xamarin.Forms.VisualElement element, Android.Support.V4.App.FragmentManager fragmentManager) [0x00031] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\Platform.cs:325 11-24 14:30:33.323 I/MonoDroid( 4272): at Xamarin.Forms.Platform.Android.AppCompat.FragmentContainer.OnCreateView (Android.Views.LayoutInflater inflater, Android.Views.ViewGroup container, Android.OS.Bundle savedInstanceState) [0x00008] in C:\BuildAgent2\work\aad494dc9bc9783\Xamarin.Forms.Platform.Android\AppCompat\FragmentContainer.cs:66 11-24 14:30:33.323 I/MonoDroid( 4272): at Android.Support.V4.App.Fragment.n_OnCreateView_Landroid_view_LayoutInflater_Landroid_view_ViewGroup_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_inflater, System.IntPtr native_container, System.IntPtr native_savedInstanceState) [0x00022] in <27c17fe440cf491ba8255bcefade6e02>:0 11-24 14:30:33.323 I/MonoDroid( 4272): at (wrapper dynamic-method) System.Object:6fd6c39c-2adb-4745-a0e9-f3e78f947f9c (intptr,intptr,intptr,intptr,intptr) 11-24 14:30:33.323 D/Mono ( 4272): Assembly Ref addref HockeySDK.AndroidBindings[0xb954fd20] -> System[0xb967d5c0]: 18

Any ideas how to solve it.

Answers

  • NicholasBauerNicholasBauer USMember ✭✭

    I think I have the solution: You need to inherit from MasterDetailPageRenderer in Xamarin.Forms.Platform.Android.AppCompat instead.

Sign In or Register to comment.