c# - Xamarin iOS 应用程序中的空引用异常

标签 c# ios xamarin xamarin.ios nullreferenceexception

我正在尝试在我的应用程序中设置全局变量,例如颜色。即导航栏,我必须在每个 View Controller 中单独设置的颜色。 我创建了一个辅助类 UIHelper.cs 来设置这些值,然后在别处调用它们。 在我的 MainViewController.cs 中,我创建了一个 UIHelper 的实例并在这一行调用它: this.NavigationController.NavigationBar.BarTintColor = uiHelper.SetNavBarRGB();

但这会引发 System.NullReferenceException

System.NullReferenceException: Object reference not set to an instance of an object
  at NoteTaker.iOS.MainViewController.ViewDidLoad () [0x00018] in /Users/richardcurteis/Development/XamarinProjects/NoteTaker/iOS/MainViewController_.cs:28
  at at (wrapper managed-to-native) UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, IntPtr principal, IntPtr delegate) [0x00005] in /Users/builder/data/lanes/2377/73229919/source/maccore/src/UIKit/UIApplication.cs:77
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x00038] in /Users/builder/data/lanes/2377/73229919/source/maccore/src/UIKit/UIApplication.cs:61
  at NoteTaker.iOS.Application.Main (System.String[] args) [0x00013] in /Users/richardcurteis/Development/XamarinProjects/NoteTaker/iOS/Main.cs:17

MainViewController.cs

using System;
using System.CodeDom.Compiler;
using UIKit;
using System.Collections.Generic;

namespace NoteTaker.iOS
{
    partial class MainViewController : UIViewController
    {
        List<Note> notes;
        UITableView table;
        UIHelper uiHelper;


        public MainViewController (IntPtr handle) : base (handle)
        {
            notes = new List<Note> ();
            UIHelper uiHelper = new UIHelper();
        }

        public override void ViewDidLoad ()

        {
            base.ViewDidLoad ();

            //this.NavigationController.NavigationBar.BarTintColor = UIColor.FromRGB (255,0,255);
            this.NavigationController.NavigationBar.BarTintColor = uiHelper.SetNavBarRGB(); // Exception being thrown here
            this.Title = "Notes";
            this.NavigationController.NavigationBar.TitleTextAttributes = new UIStringAttributes () { ForegroundColor = UIColor.White };

            table = new UITableView () {
                Frame = new CoreGraphics.CGRect (0, this.NavigationController.NavigationBar.Frame.Bottom, this.View.Bounds.Width, this.View.Bounds.Height - this.NavigationController.NavigationBar.Frame.Height),
            };
            this.View.Add (table);

            var addButton = new UIBarButtonItem (UIBarButtonSystemItem.Add);
            addButton.TintColor = UIColor.White;

            this.NavigationItem.RightBarButtonItems = new UIBarButtonItem[] { addButton };
            addButton.Clicked += (sender, e) => {
                this.NavigationController.PushViewController (new NoteViewController(), true);
            };

            notes = Database.getNotes ();
            table.Source = new NotesTableViewSource (notes, this);
        }

        public override void ViewWillAppear (bool animated)
        {
            base.ViewWillAppear (animated);
            notes = Database.getNotes ();
            table.Source = new NotesTableViewSource(notes, this );
        }
    }
}

UIHelper.cs

using System;
using UIKit;

namespace NoteTaker.iOS
{
    public class UIHelper : UIInputViewController
    {
        public UIHelper ()
        {
        }

        public UIColor SetNavBarRGB ()
        {
            var uiColour = UIColor.FromRGB (255, 0, 255);
            return uiColour;
        }
    }
}

最佳答案

你有冲突:

您将 uiHelper 声明为类定义中的一个字段:

partial class MainViewController : UIViewController
{
    List<Note> notes;
    UITableView table;
    UIHelper uiHelper;

然后在构造函数中声明另一个同名变量:

public MainViewController (IntPtr handle) : base (handle)
{
        notes = new List<Note> ();
        UIHelper uiHelper = new UIHelper();
}

这导致局部 uiHelper 仅在构造函数的范围内声明和分配,但从未触及类的字段 uiHelper。因此它在 ViewLoad() 方法中为 null。只需删除构造函数中的类型即可:

public MainViewController (IntPtr handle) : base (handle)
{
        notes = new List<Note> ();
        uiHelper = new UIHelper();
}

关于c# - Xamarin iOS 应用程序中的空引用异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35004030/

相关文章:

xamarin 表单与 mvvmcross 一起使用

c# - 如何为具有身份列的 NHibernate 实体实现 GetHashCode?

ios - 缩小弹出窗口的内容大小时防止伪影

c# - ASP.NET 按钮点击重定向到新页面

ios - 有没有办法在保持同一页面的同时从 UIPageViewController 中更改标签文本?

javascript - PhoneGap : JavaScript Focus and Blur event in iOS/Android

c# - Xamarin-在Gridview中创建一个额外的列,其垂直跨度为2

ios - 检查 secret 文件是否存在

c# - 如何使用 mblox 通过 SMPP 发送短信?

c# - 如何检测二值图像RGB中的圆?