选中行 - 空引用异常

3

我试图实现的功能是,当我在UITableView上按下一行时,它将向另一个UIViewController发送一些数据并进行转场。

然而,在测试时,它给了我以下错误:

System.NullReferenceException has been thrown Object reference not set to an instance of an object

错误似乎出现在这里:

viewController.NavigationController.PushViewController (detail, true);

这是我的“RowSelected”

public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
    Console.WriteLine ("Row: " + tableItems [indexPath.Row].Heading);

    //new UIAlertView("Row Selected", tableItems[indexPath.Row].Heading, null, "OK", null).Show();
    tableView.DeselectRow (indexPath, true);

    // Specially for Storyboard !!
    var detail = viewController.Storyboard.InstantiateViewController("detail") as HolsteinItemViewController;
    detail.Title = tableItems[indexPath.Row].Heading;
    detail.LoadUrl (tableItems[indexPath.Row].SubHeading);
    viewController.NavigationController.PushViewController (detail, true);
}

捕获到异常:

System.NullReferenceException: Object reference not set to an instance of an object
  at Holstein.TableSource.RowSelected (MonoTouch.UIKit.UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath) [0x0008e] in /Users/Emil/Projects/Holstein/Holstein/Code/TableSource.cs:41
  at at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
  at Holstein.Application.Main (System.String[] args) [0x00008] in /Users/Emil/Projects/Holstein/Holstein/Main.cs:16

TableSource.cs第46行

viewController.NavigationController.PushViewController (detail, true);

Main.cs 第16行

UIApplication.Main (args, null, "AppDelegate");

tableItems[indexPath.Row].Heading和Subheading都不为null吗? - user1646737
两者都应该设置,因为它们都显示在单元格中。 - Emil Elkjær
还有哪些期望但未定义的详细属性吗? - user1646737
2
如果在尝试推送控制器时抛出了空引用异常,那么很可能是“viewController”的NavigationController属性为空。该控制器是否实际上被推送到导航控制器上?它在此时可见吗,还是刚刚实例化? - Dimitris Tavlikos
添加了捕获的异常。 - Emil Elkjær
显示剩余2条评论
1个回答

4
您的viewController.NavigationController属性为空。根据iOS UIViewController参考这里所述,如果接收器或其祖先之一是导航控制器的子级,则此属性包含拥有导航控制器。如果视图控制器未嵌入导航控制器中,则此属性为nil。 显然,该视图控制器不是UINavigationController堆栈的一部分。

你如何将它添加到堆栈中? - testing
1
通过调用UINavigationController的PushViewController方法。 - Dimitris Tavlikos
请回答这个问题:http://stackoverflow.com/questions/36324571/object-reference-not-set-to-an-instance-of-an-object-in-monotouch - Alok Rajasukumaran

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接