2016-01-25 41 views
1

我想在我的應用程序中設置全局變量,如顏色。即導航欄,我必須在每個視圖控制器中單獨設置的顏色。 我創建了一個輔助類來設置這些值,然後在別處調用它們。 在我MainViewController.cs,我創建的UIHelper的實例並調用它在這條線: this.NavigationController.NavigationBar.BarTintColor = uiHelper.SetNavBarRGB();Xamarin iOS應用程序中的空引用異常

但這扔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; 
     } 
    } 
} 

回答

2

您有衝突:

您聲明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()方法中爲空。只需刪除構造函數中的類型即可:

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

難道你MainViewController構造函數(MainViewController (IntPtr handle))甚至被調用?

您可以試着針對變量檢查null,如果它爲空,則從UIHelper類初始化一個新實例。

public override void ViewDidLoad() 

     { 
      base.ViewDidLoad(); 
      If(uiHelper == null) 
      { 
       uiHelper = new UIHelper(); 
      } 
      //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); 
     } 
+0

這是一個很好的觀點。當一個*存在的本地實例出現在被管理端時,調用'.ctor(IntPtr)'。它很少是放置初始化代碼的好地方,並且如果僅由託管代碼創建,它可能永遠不會被調用。對於本地類型的子類,最好查看它們的生命週期(例如,UIView和UIViewController有充分的文檔記錄),因爲當你可以做某些事情時(以及它們做得太早或太遲時),它往往會說清楚它們。 – poupou

+0

@poupou,你會有什麼建議作爲替代? –