【问题标题】:iOS app crashes when I click on my UIButton当我单击我的 UIButton 时,iOS 应用程序崩溃
【发布时间】:2013-10-28 02:22:21
【问题描述】:

总结

这是我尝试使用 Xamarin 制作的第一个应用程序。我正在从Hello World, iPhone 示例中学习。当我点击我的按钮时,应用程序崩溃了,我不知道为什么。

详情

我有一个简单的UIViewController 和一个UIButton。我已经创建了一个插座,所以当我点击按钮时,我会做一些事情。

当我连接按钮的TouchUpInside event 时,当我单击按钮时应用程序崩溃。当我删除连接事件(但插座仍然存在)时,单击按钮时应用程序不会崩溃。 (当然,没有任何事情发生,因为没有连接任何东西)。

令我沮丧的是,我不知道出了什么问题,使用调试信息,我无法弄清楚!多年来我一直在编写 C# 代码,但刚开始使用 Xam 有点不同,尤其是当崩溃报告缺少任何信息时。我猜我可能勾选了一些设置(例如,没有调试信息?)

所以这里有一些代码......

AuthenticationViewController
.h 文件

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>


@interface AuthenticationViewController : UIViewController {
UIButton *_Google;
}

@property (nonatomic, retain) IBOutlet UIButton *Google;

@end

.m 文件

#import "AuthenticationViewController.h"

@implementation AuthenticationViewController

@synthesize Google = _Google;

@end

设计器文件

using MonoTouch.Foundation;
using System.CodeDom.Compiler;

namespace Foo.Client.iPhone
{

    [Register ("AuthenticationViewController")]
    partial class AuthenticationViewController
    {
        [Outlet]
        MonoTouch.UIKit.UIButton Google { get; set; }

        void ReleaseDesignerOutlets ()
        {
            if (Google != null) {
                Google.Dispose ();
                Google = null;
            }
        }
    }
}

最后,我的

.cs 文件

using System;
using System.Drawing;
using MonoTouch.Foundation;
using MonoTouch.UIKit;

namespace Foo.Client.iPhone
{
    public partial class AuthenticationViewController : UIViewController
    {
        public int xxx = 0;
        public event EventHandler OnAuthenticationCompleted;

        public AuthenticationViewController() 
            : base ("AuthenticationViewController", null)
        {
        }

        public override void DidReceiveMemoryWarning()
        {
            // Releases the view if it doesn't have a superview.
            base.DidReceiveMemoryWarning();

            // Release any cached data, images, etc that aren't in use.
        }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            Google.TouchUpInside += (sender, e) => 
            {
                xxx++;
            };
        }
    }
}

这是(缺少的)故障转储/堆栈跟踪

2013-10-25 21:37:13.785 FooClientiPhone[8852:1403] MonoTouch: Socket error while connecting to MonoDevelop on 127.0.0.1:10000: Connection refused
mono-rt: Stacktrace:

mono-rt:   at <unknown> <0xffffffff>

mono-rt:   at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <IL 0x0009f, 0xffffffff>

mono-rt:   at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38

mono-rt:   at Foo.Client.iPhone.Application.Main (string[]) [0x00008] in /Users/PureKrome/Documents/Mac Projects/Foo iOS Client/Code/Foo.Client.iPhone/Main.cs:16

mono-rt:   at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>

mono-rt: 
    Native stacktrace:

 mono-rt: 
    =================================================================
    Got a SIGSEGV while executing native code. This usually indicates
        a fatal error in the mono runtime or one of the native libraries 
            used by your application.
            =================================================================

我卡住了,不知道如何继续?

还有,here's a video showing the error(请选择 720p 版本)。

这是solution, zipped up

【问题讨论】:

  • 在不调试的情况下运行应用是否会出现崩溃?
  • 是的。它确实也会发生。

标签: c# .net objective-c xcode xamarin


【解决方案1】:

从 xCode 的角度来看,我认为 IBOutlet 导致您的应用程序崩溃。仅仅是因为 IBOutlets 用于状态而不是用于调用方法。在这种情况下,您应该将按钮连接到 IBAction:

- (IBAction)Google:(id)sender; 

最终会调用:

Google.TouchUpInside += (sender, e) => 
            {
                xxx++;
            };

另外,请注意,在现代 Objective-c 中,您不需要 @synthesize Google = _Google;,这会自动发生。

快乐的 x(amarin) 编码。

【讨论】:

    【解决方案2】:

    这告诉我们也许我们正在失去对某些东西的引用。我知道有人已经说过要启用僵尸,但这可能不是您的包装器/编译器的选项。

    mono-rt:   at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>
    

    尝试替换:

    @interface AuthenticationViewController : UIViewController {
        UIButton *_Google; 
    }
    
    @property (nonatomic, retain) IBOutlet UIButton *Google;
    

    只需:

    @interface AuthenticationViewController : UIViewController 
    @property (nonatomic, retain) IBOutlet UIButton *Google;
    

    也许当你点击那个按钮时编译器不知道你在说什么。

    【讨论】:

      【解决方案3】:

      你为一个简单的 hello world 写了很多代码

      这是你不需要的

      @property (nonatomic, retain) IBOutlet UIButton *Google;
      @synthesize Google = _Google;
      
      using  MonoTouch . Foundation ; 
      using  System . CodeDom . Compiler ; 
      
      namespace  Foo . Client . iPhone
      { 
      
      [ Register  ( "AuthenticationViewController" )] 
      partial  class  AuthenticationViewController 
      { 
          [ Outlet ] 
          MonoTouch . UIKit . UIButton  Google  {  get ;  set ;  } 
      
          void  ReleaseDesignerOutlets  () 
          { 
              if  ( Google  !=  null )  { 
                  Google . Dispose  (); 
                  Google  =  null ; 
              } 
             } 
          } 
         }
        using System;
        using System.Drawing;
        using MonoTouch.Foundation;
        using MonoTouch.UIKit;
      
        namespace Foo.Client.iPhone
       {
       public partial class AuthenticationViewController : UIViewController
       {
          public int xxx = 0;
          public event EventHandler OnAuthenticationCompleted;
      
          public AuthenticationViewController() 
              : base ("AuthenticationViewController", null)
          {
          }
      
          public override void DidReceiveMemoryWarning()
          {
              // Releases the view if it doesn't have a superview.
              base.DidReceiveMemoryWarning();
      
              // Release any cached data, images, etc that aren't in use.
          }
      
          public override void ViewDidLoad()
          {
              base.ViewDidLoad();
      
              Google.TouchUpInside += (sender, e) => 
              {
                  xxx++;
              };
          }
         }
         } 2013-10-25 21:37:13.785 FooClientiPhone[8852:1403] MonoTouch: Socket error while           connecting to MonoDevelop on 127.0.0.1:10000: Connection refused
          mono-rt: Stacktrace:
      
         mono-rt:   at <unknown> <0xffffffff>
      
        mono-rt:   at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain         (int,string[],intptr,intptr) <IL 0x0009f, 0xffffffff>
      
        mono-rt:   at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
      
        mono-rt:   at Foo.Client.iPhone.Application.Main (string[]) [0x00008] in /Users/PureKrome/Documents/Mac Projects/Foo iOS Client/Code/Foo.Client.iPhone/Main.cs:16
      
      mono-rt:   at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object          (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>
      
         mono-rt: 
         Native stacktrace:
        mono-rt: 
         =================================================================
          Got a SIGSEGV while executing native code. This usually indicates
          a fatal error in the mono runtime or one of the native libraries 
              used by your application.
              =================================================================
      

      所以如果你用一个按钮创建一个 hello world 就不需要这个了

      打造一个hello world只需要这个

      @interface ViewController ()
      
      
      @property (nonatomic, strong) UILabel *myLabel;
      
      
      
      @implementation ViewController
      
      - (IBAction)myButton:(id)sender 
      { 
        _myLabel.Text = @"Hello World ";
      }
      @end
      

      【讨论】:

      • 删除不必要的代码总是好的。但也许提问者需要其中的一些,这导致了崩溃。关于可能导致崩溃的任何想法或 cmet?
      猜你喜欢
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      • 2022-06-11
      • 2017-10-23
      • 2018-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多