应用导航栏按钮事件时应用崩溃

时间:2011-04-08 19:08:55

标签: iphone ios xamarin.ios

在我的应用程序中,我有一个导航栏按钮,允许用户弹回导航堆栈的顶部。这已经有一段时间了,一直很好。但是,当我使用MT 4.0进行构建时,它会崩溃。

# in constructor
UIButton btn = UIButton.FromType(UIButtonType.Custom);
btn.Frame = new RectangleF(0,0,29,29);
btn.SetBackgroundImage(UIImage.FromFile("images/Home_button.png"),UIControlState.Normal);
btn.TouchDown += delegate {
  this.NavigationController.PopToRootViewController(true);
};
UIBarButtonItem bar = new UIBarButtonItem(btn);
this.NavigationItem.RightBarButtonItem = bar;           

按钮绘制正确,但按下按钮时应用程序崩溃。如果我将委托更改为仅执行Console.WriteLine(),它仍然会崩溃。

据我所知,这是在我安装MT 4.0之前一直在努力。

崩溃时显示的异常是

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object   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) [0x00038] in 
/Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:26 at 
Application.Main (System.String[] args) [0x00000] in 
/Users/jason/Projects/myproj/myproj/AppDelegate.cs:44

使用后退按钮导航正常。这是一个错误,还是我做错了什么?

2 个答案:

答案 0 :(得分:2)

快速提问,但是我问这个是因为我多次对自己这样做了,对你的View的引用,它不是偶然在你的Main方法中的本地范围,是吗?会发生什么事情是参考丢失但屏幕仍然出现但是一旦你想做某事就被垃圾收集了。

“后退”按钮由导航控制器本身拥有,您可能已经通过Window引用了该按钮,因此它可以完全通过您。

答案 1 :(得分:0)

就像快速提醒一样,当没有在本地变量上创建按钮但是包含按钮的视图时,我发生了类似的崩溃。我已经描述了解决方案here

相关问题