Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

NullReferenceException on MasterDetailPage

$
0
0

Hi Guys. I'm currently getting this error when loading MasterDetailPage. I'm updated to the latest version of Xamarin.Forms 1.2.2.6243

System.TypeLoadException: A type load exception has occurred.
at TEST.App.GetMainPage () [0x00001] in c:\Users\Ian\Documents\Projects\TEST\TEST\App.cs:12
at TEST.Android.MainActivity.OnCreate (Android.OS.Bundle) [0x00011] in c:\Users\Ian\Documents\Projects\TEST\Android\MainActivity.cs:25
at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.14-series/a5d57087/source/monodroid/src/Mono.Android/platforms/android-15/src/generated/Android.App.Activity.cs:1944
at at (wrapper dynamic-method) object.ce62ca0a-a871-4aa1-81ad-2dcfa97f1a03 (intptr,intptr,intptr)

I'm getting the error in both my application and this test application

using System;
using Xamarin.Forms;
using System.Collections.Generic;
using System.Linq;

namespace TEST
{
    public class App
    {
        public static Page GetMainPage ()
        {   
            return  new MasterDetailPageDemoPage ();

        }
    }



    class MasterDetailPageDemoPage :  MasterDetailPage
    {
        public MasterDetailPageDemoPage()
        {
            Label header = new Label
            {
                Text = "MasterDetailPage",
                Font = Font.BoldSystemFontOfSize(30),
                HorizontalOptions = LayoutOptions.Center
            };

            // Assemble an array of NamedColor objects.
            Dictionary<string,Color> namedColors = new Dictionary<string, Color> ();

            namedColors.Add ("Aqua", Color.Aqua);
            namedColors.Add ("Black", Color.Black);
            namedColors.Add ("Blue", Color.Blue);
            namedColors.Add ("Fuschia", Color.Fuschia);
            namedColors.Add ("Gray", Color.Gray);
            namedColors.Add ("Green", Color.Green);
            namedColors.Add ("Lime", Color.Lime);
            namedColors.Add ("Maroon", Color.Maroon);
            namedColors.Add ("Navy", Color.Navy);
            namedColors.Add ("Olive", Color.Olive);
            namedColors.Add ("Purple", Color.Purple);
            namedColors.Add ("Red", Color.Red);
            namedColors.Add ("Silver", Color.Silver);
            namedColors.Add ("Teal", Color.Teal);
            namedColors.Add ("White", Color.White);
            namedColors.Add ("Yellow", Color.Yellow);

            // Create ListView for the master page.
            ListView listView = new ListView
            {
                ItemsSource = namedColors
            };

            // Create the master page with the ListView.
            this.Master = new ContentPage
            {
                Content = new StackLayout
                {
                    Children = 
                    {
                        header, 
                        listView
                    }
                    }
            };

            // Create the detail page using NamedColorPage
            this.Detail = new NamedColorPage(true);

            // For Windows Phone, provide a way to get back to the master page.
            if (Device.OS == TargetPlatform.WinPhone)
            {
                (this.Detail as ContentPage).Content.GestureRecognizers.Add(
                    new TapGestureRecognizer((view) =>
                        {
                            this.IsPresented = true;
                        }));
            }

            // Define a selected handler for the ListView.
            listView.ItemSelected += (sender, args) =>
            {
                // Set the BindingContext of the detail page.
                this.Detail.BindingContext = args.SelectedItem;

                // Show the detail page.
                this.IsPresented = false;
            };

            // Initialize the ListView selection.
            listView.SelectedItem = namedColors.First();


        }
    }


    // Used in:
    //      MasterDetailPageDemoPage (as a page)
    //      TabbedPageDemoPage (as a page template)
    //      CarouselPageDemoPage (as a page template)
    //
    //  Expects BindingContext to be of type NamedColor!
    class NamedColorPage : ContentPage
    {
        public NamedColorPage(bool includeBigLabel)
        {
            // This binding is necessary to label the tabs in 
            //      the TabbedPage.
            this.SetBinding(ContentPage.TitleProperty, "Name");

            // BoxView to show the color.
            BoxView boxView = new BoxView
            {
                WidthRequest = 100,
                HeightRequest = 100,
                HorizontalOptions = LayoutOptions.Center
            };
            boxView.SetBinding(BoxView.ColorProperty, "Color");

            // Function to create six Labels.
            Func<string, string, Label> CreateLabel = (string source, string fmt) =>
            {
                Label label = new Label
                {
                    Font = Font.SystemFontOfSize(NamedSize.Large),
                    XAlign = TextAlignment.End
                };
                label.SetBinding(Label.TextProperty,
                    new Binding(source, BindingMode.OneWay, null, null, fmt));

                return label;
            };

            // Build the page
            this.Content = new StackLayout
            {
                Children = 
                {
                    new StackLayout
                    {   
                        HorizontalOptions = LayoutOptions.Center,
                        VerticalOptions = LayoutOptions.CenterAndExpand,
                        Children = 
                        {
                            CreateLabel("Color.R", "R = {0:F2}"),
                            CreateLabel("Color.G", "G = {0:F2}"),
                            CreateLabel("Color.B", "B = {0:F2}"),
                        }
                        },
                    boxView,
                    new StackLayout
                    {   
                        HorizontalOptions = LayoutOptions.Center,
                        VerticalOptions = LayoutOptions.CenterAndExpand,
                        Children = 
                        {
                            CreateLabel("Color.Hue", "Hue = {0:F2}"),
                            CreateLabel("Color.Saturation", "Saturation = {0:F2}"),
                            CreateLabel("Color.Luminosity", "Luminosity = {0:F2}")
                        }
                        }
                }
                };

            // Add in the big Label at top for CarouselPage.
            if (includeBigLabel)
            {
                Label bigLabel = new Label
                {
                    Font = Font.SystemFontOfSize(50),
                    HorizontalOptions = LayoutOptions.Center
                };
                bigLabel.SetBinding(Label.TextProperty, "Name");

                (this.Content as StackLayout).Children.Insert(0, bigLabel);
            }
        }
    }

}



Can anybody please help me. Thanks in advance

Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>