处理导航栏按钮事件时,应用程序崩溃 [英] app crashes when handling navbar button event

查看:105
本文介绍了处理导航栏按钮事件时,应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my app, I have a navbar button that allows the user to pop back to the top of the navigation stack. This has been there for a while and has been working fine. However, when I build with MT 4.0 it crashes.

# 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()它仍然会崩溃。

The button is drawn correctly, but the app crashes when the button is pressed. If I change the delegate to just do a Console.WriteLine() it still crashes.

据我所知,这一直在用,直到我安装MT 4.0。

As far as I can tell, this was working up until I installed 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

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

Navigating using the back button is working normally. Is this a bug, or am I doing something wrong?

推荐答案

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

Quick question, but I ask this because I did this to myself several times, the reference to your View, it isn't locally scoped in your Main's method by chance, is it? What happens is that the reference is lost but the screen still appears but then as soon as you want to do something it's been garbage collected.

后退按钮属于导航控制器本身,可能会被你的Window引用,因此它可以真正通过你。

The Back button is owned by the navigation controller itself, which would have been referenced likely by your Window, and therefore it can really through you off.

这篇关于处理导航栏按钮事件时,应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆