Xamarin形式:AppDelegate中的System.NullReferenceException

时间:2018-06-19 12:09:23

标签: xamarin xamarin.forms

您好尝试第一次为IOS编译我的应用程序,但我知道了:

  

System.NullReferenceException:对象引用未设置为对象的实例

在AppDelegate中

。 它在此行中停止:return base.FinishedLaunching(app, options);

我是xamarin的新手,所以很抱歉我不知情。

这是我完整的AppDelegate:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using System.Globalization;
using System.IO;
using Flex;
using Foundation;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using System.Xml.Linq;

namespace dpsApp.iOS
{

[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {



        global::Xamarin.Forms.Forms.Init();
        Rg.Plugins.Popup.Popup.Init();
        FlexButton.Init();
        LoadApplication(new App());

        return base.FinishedLaunching(app, options);
    }
}

}

这是我的MainPage XAML:  `                                                                                                                               

        <StackLayout WidthRequest="10000">
            <StackLayout x:Name="LogoStack" BackgroundColor="White">
                <Image x:Name="Image" 
                        Source="a.png"
                        HeightRequest="120"
                        WidthRequest="120"
                        HorizontalOptions="Center">

                    <Image.GestureRecognizers>
                        <TapGestureRecognizer Tapped="EasterCount"/>
                    </Image.GestureRecognizers>
                </Image>

            </StackLayout>

            <StackLayout x:Name="StackList"  IsVisible="True" HeightRequest="3000">
                <ListView x:Name="PageList" 
                      HasUnevenRows="True"
                      ItemTapped="Link_ItemTapped"
                      HeightRequest="25"
                      BackgroundColor="White">

                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <StackLayout Orientation="Horizontal" Margin="0,0,0,0" BackgroundColor="#e7e7e7" HeightRequest="65" Padding="0">

                                    <Grid x:Name="DeleteStack" VerticalOptions="CenterAndExpand" BackgroundColor="White" HorizontalOptions="FillAndExpand" >
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" />
                                            <ColumnDefinition Width="50" />
                                        </Grid.ColumnDefinitions>

                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="28" />
                                            <RowDefinition Height="22" />
                                            <RowDefinition Height="1" />
                                        </Grid.RowDefinitions>

                                        <Label Grid.Column="0" Grid.Row="0" Text="{Binding Title}" LineBreakMode="TailTruncation" FontSize="25" Margin="20,0,0,0"/>
                                        <Label Grid.Column="0" Grid.Row="1" Text="{Binding Link}" LineBreakMode="TailTruncation" FontSize="17" Margin="20,0,0,0"/>

                                        <Image Margin="0,0,20,0"  
                                                   IsVisible="{Binding IsVisible}" 
                                                   Grid.Column="1" 
                                                   Grid.Row="0" 
                                                   Grid.RowSpan="2" 
                                                   x:Name="DeleteButton"                                                        
                                                   Source="delete.png" 
                                                   VerticalOptions="Center"
                                                   HeightRequest="20" 
                                                   HorizontalOptions="Center"/>

                                    </Grid>

                                </StackLayout>
                            </ViewCell>

                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

            </StackLayout>
            <StackLayout x:Name="FirstTimeOpenStack" HorizontalOptions="Center">
                <Label Text="Please tap on the plus icon in the top right corner to add a website" />

            </StackLayout>

        </StackLayout>

1 个答案:

答案 0 :(得分:1)

您的App委托看起来不错。您正在启动的App()中某个地方有未处理的异常。

这是捕获未处理异常的一种方法:

namespace WiFiVisualPager.iOS
{
    // The UIApplicationDelegate for the application. This class is responsible for launching the 
    // User Interface of the application, as well as listening (and optionally responding) to 
    // application events from iOS.
    [Register("AppDelegate")]
    public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
    {
        //
        // This method is invoked when the application has loaded and is ready to run. In this 
        // method you should instantiate the window, load the UI into it and then make the window
        // visible.
        //
        // You have 17 seconds to return from this method, or iOS will terminate your application.
        //
        public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException;
            TaskScheduler.UnobservedTaskException += TaskSchedulerOnUnobservedTaskException;

            global::Xamarin.Forms.Forms.Init();
            DisplayCrashReport();

            LoadApplication(new App());

            return base.FinishedLaunching(app, options);
        }

        #region [Error handling]
        //Credit: Peter Norman.
        //https://peterno.wordpress.com/2015/04/15/unhandled-exception-handling-in-ios-and-android-with-xamarin/
        //Minor compile fixes by David McCurley.

        private static void TaskSchedulerOnUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs unobservedTaskExceptionEventArgs)
        {
            var newExc = new Exception("TaskSchedulerOnUnobservedTaskException", unobservedTaskExceptionEventArgs.Exception);
            LogUnhandledException(newExc);
        }

        private static void CurrentDomainOnUnhandledException(object sender, UnhandledExceptionEventArgs unhandledExceptionEventArgs)
        {
            var newExc = new Exception("CurrentDomainOnUnhandledException", unhandledExceptionEventArgs.ExceptionObject as Exception);
            LogUnhandledException(newExc);
        }

        internal static void LogUnhandledException(Exception exception)
        {
            try
            {
                const string errorFileName = "Fatal.log";
                var libraryPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Resources); // iOS: Environment.SpecialFolder.Resources
                var errorFilePath = Path.Combine(libraryPath, errorFileName);
                var errorMessage = String.Format("Time: {0}\r\nError: Unhandled Exception\r\n{1}",
                DateTime.Now, exception.ToString());
                File.WriteAllText(errorFilePath, errorMessage);

                // Log to Android Device Logging.
                //Android.Util.Log.Error("Crash Report", errorMessage);
            }
            catch
            {
                // just suppress any error logging exceptions
            }
        }

        /// <summary>
        // If there is an unhandled exception, the exception information is diplayed 
        // on screen the next time the app is started (only in debug configuration)
        /// </summary>
        [Conditional("DEBUG")]
        private static void DisplayCrashReport()
        {
            const string errorFilename = "Fatal.log";
            var libraryPath = Environment.GetFolderPath(Environment.SpecialFolder.Resources);
            var errorFilePath = Path.Combine(libraryPath, errorFilename);

            if (!File.Exists(errorFilePath))
            {
                return;
            }

            var errorText = File.ReadAllText(errorFilePath);
            var alertView = new UIAlertView("Crash Report", errorText, null, "Close", "Clear") { UserInteractionEnabled = true };
            alertView.Clicked += (sender, args) =>
            {
                if (args.ButtonIndex != 0)
                {
                    File.Delete(errorFilePath);
                }
            };
            alertView.Show();
        }
        #endregion
    }
}