尝试实例化新的ViewController时抛出System.Reflection.TargetInvocationException

时间:2014-08-12 08:23:45

标签: c# ios uiviewcontroller xamarin.ios xamarin

我有一个故事板。因为我在代码中加载了一个视图(取决于条件),我还没有使用过segue。现在我想加载另一个视图,但我收到以下错误:

  

抛出了System.Reflection.TargetInvocationException   调用目标引发了异常。

ListButton.TouchUpInside += (object sender, EventArgs e) => {
    MyListController myListController = this.Storyboard.InstantiateViewController ("MyListController") as MyListController;
    myListController.EdgesForExtendedLayout = UIRectEdge.None;
    if (myListController != null) {
        this.NavigationController.PushViewController (myListController, true);
    }
};

按下按钮(ListButton)时,应将视图加载到导航堆栈中。 InstantiateViewController发生错误。

我该如何解决?

修改

我进入MyListController,如果我评论以下内容,则视图会被加载:

public MyListController (IntPtr handle) : base (handle)
{
    TableView.RegisterClassForCellReuse (typeof(UITableViewCell), listCellId);
    TableView.Source = new MyListDataSource (this);
    MyList = new List<string> ();
}

日志:

  

System.Reflection.TargetInvocationException:抛出了异常   通过调用的目标。 ---&GT; System.Exception:Object   引用未设置为对象的实例   TestApp.MyListController.ViewDidLoad()[0x0004c] in   /Users/xxx/Projects/TestApp/TestApp/TestApp/MyListController.cs:78
  at at(包装器管理到原生)   MonoTouch.ObjCRuntime.Messaging:IntPtr_objc_msgSendSuper   (intptr,intptr)at   MonoTouch.UIKit.UITableViewController.get_TableView()[0x00000] in   :TestApp.MyListController..ctor上的0(IntPtr   处理)[0x00009] in   /Users/xxx/Projects/TestApp/TestApp/TestApp/MyListController.cs:17
  at at(包装器管理到原生)   System.Reflection.MonoCMethod:InternalInvoke   (System.Reflection.MonoCMethod,object,object [],System.Exception&amp;)at   System.Reflection.MonoCMethod.InternalInvoke(System.Object obj,   System.Object []参数)[0x00002] in   /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:537   ---内部异常堆栈跟踪的结束---在System.Reflection.MonoCMethod.InternalInvoke(System.Object obj,   System.Object []参数)[0x00016] in   /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:543   在System.Reflection.MonoCMethod.DoInvoke(System.Object obj,   BindingFlags invokeAttr,System.Reflection.Binder binder,   System.Object []参数,System.Globalization.CultureInfo文化)   [0x00095] in   /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:528   在System.Reflection.MonoCMethod.Invoke(BindingFlags invokeAttr,   System.Reflection.Binder binder,System.Object []参数,   System.Globalization.CultureInfo culture)[0x00000] in   /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:556   在System.Reflection.ConstructorInfo.Invoke(System.Object []   参数)[0x00000] in   /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Reflection/ConstructorInfo.cs:62   在MonoTouch.ObjCRuntime.Runtime.ConstructNSObject [NSObject](IntPtr   ptr,System.Type类型,MissingCtorResolution missingCtorResolution)   [0x00000] in:0 at   MonoTouch.ObjCRuntime.Runtime.ConstructNSObject(IntPtr ptr,IntPtr   klass,MissingCtorResolution missingCtorResolution)[0x00000] in   :MonoTouch.ObjCRuntime.Runtime.GetNSObject为0   (IntPtr ptr,MissingCtorResolution missingCtorResolution)[0x00000] in   :MonoTouch.ObjCRuntime.Runtime.GetNSObject为0   (IntPtr ptr)[0x00000] in:0 at   MonoTouch.UIKit.UIStoryboard.InstantiateViewController(System.String   标识符)[0x00000] in:0 at   TestApp.StartScreenViewController.m__1(System.Object   sender,System.EventArgs e)[0x0000c] in   /Users/xxx/Projects/TestApp/TestApp/TestApp/StartScreenViewController.cs:57   在MonoTouch.UIKit.UIControlEventProxy.Activated()[0x00000]中   :0 at at(包装器托管到原生)   MonoTouch.UIKit.UIApplication:UIApplicationMain   (int,string [],intptr,intptr)在MonoTouch.UIKit.UIApplication.Main   (System.String [] args,System.String principalClassName,System.String   delegateClassName)[0x00000] in:0 at   TestApp.Application.Main(System.String [] args)[0x00008] in   /Users/xxx/Projects/TestApp/TestApp/TestApp/Main.cs:17

尝试:
嗯,现在我在故事板中添加了一个新的UITableViewController,将一些类更改为public,将变量名更改为以小写字母开头。此外,旧的UITableviewController在添加的SearchController时出现问题。现在我把所有新东西都做了,问题只依赖于viewDidLoad但这不是原来的问题。这是其中一个步骤(可能是SearchController问题)。

有问题的代码:

this.NavigationController.ToolbarHidden = false;

他似乎无法找到NavigationController ...如何将导航控制器添加到新创建的视图中?

0 个答案:

没有答案