Skip to main content
 首页 » 编程设计

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

2024年05月10日37langtianya

我正在尝试在我的应用程序中设置全局变量,例如颜色。即导航栏,我必须在每个 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(); 
}