ios : Nullpointer exception

标签 ios animation crash

我得到了以下代码:

- (void) hide_waiting_activity
{
    [UIView beginAnimations:nil context:NULL]; {
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDuration:.5];        
        self.waiting_view.alpha = 0;
        self.waiting_label.alpha = 0;
        self.activity_indicator.alpha = 0;
    } [UIView commitAnimations];
}

问题:有时会崩溃,有时不会。如果崩溃,控制台输出看起来像这样:

2013-02-07 18:16:21.658 driver[5912:907] -[NSISRestrictedToNonNegativeVariable count]: unrecognized selector sent to instance 0x1dd3aea0 2013-02-07 18:16:21.679 driver[5912:907] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSISRestrictedToNonNegativeVariable count]: unrecognized selector sent to instance 0x1dd3aea0' * First throw call stack: (0x342e03e7 0x3bfd1963 0x342e3f31 0x342e264d 0x3423a208 0x34c90d5b 0x34c9a6c3 0x34c9a56f 0x34c9a781 0x34c9acf1 0x34c99d6b 0x34c9ac31 0x34c92393 0x34c94f01 0x34c95ed7 0x34c9ecab 0x36527519 0x36527689 0x365277ab 0x3652791f 0x36527acf 0x34c95997 0x36527a41 0x3652aad1 0x3652d0d9 0x3652cfe3 0x34226acd 0x3652cf97 0x34226acd 0x3652cf97 0x34226acd 0x3652cf97 0x34226acd 0x3652cf97 0x34226acd 0x3652cf97 0x34226acd 0x3652cf97 0x34226acd 0x3652cf97 0x34226acd 0x3652cf97 0x34c95997 0x36526f3d 0x361623dd 0x35e9e513 0x35e9e0b5 0x35e9efd9 0x35e9e9c3 0x35e9e7d5 0x35e9e639 0x342b5941 0x342b3c39 0x342b3f93 0x3422723d 0x342270c9 0x37e0533b 0x361432b9 0xd3871 0xc29d8) libc++abi.dylib: terminate called throwing an exception

相应的堆栈跟踪如下所示: enter image description here

或者类似的东西:

driver(5930,0x3df85b78) malloc: * error for object 0x1e565990: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug (lldb)

然后堆栈跟踪看起来像 enter image description here

我完全不知道。有人有想法吗?我使用的是 ios 6.1。

更新: 注释内部嵌套括号不会改变任何内容:

[UIView beginAnimations:nil context:NULL]; {
        //[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        //[UIView setAnimationDuration:.5];
        self.waiting_view.alpha = 0;
        self.waiting_label.alpha = 0;
        self.activity_indicator.alpha = 0;
    } [UIView commitAnimations];

更新2: 删除大括号后,应用程序仍然按照所述崩溃。

  [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDuration:.5];
        self.waiting_view.alpha = 0;
        self.waiting_label.alpha = 0;
        self.activity_indicator.alpha = 0;
    [UIView commitAnimations];

更新3: 这是包含 hide_waiting_activity 中任何受影响的 ui 元素的完整代码:

MainNavigationController.h

//Waiting overlay
@property (nonatomic,strong) UIActivityIndicatorView* activity_indicator;
@property (nonatomic,strong) UIImageView* waiting_view;
@property (nonatomic,strong) UILabel* waiting_label;

MainNavigationController.m

@implementation MainNavigationController

@synthesize activity_indicator;
@synthesize waiting_view;
@synthesize waiting_label;

...
- (void)viewDidLoad
{
    [super viewDidLoad];

    //Create a transparent waiting indicator view that is shown to the user while the app is working in background
    waiting_view = [[UIImageView alloc] initWithFrame:CGRectMake( 0, 64.f /* top navigation bar is 64.f */, 320, 480 )];
    [waiting_view setImage:[UIImage imageNamed:@"Main_Menu_Background.png"]];

    waiting_view.backgroundColor = [UIColor clearColor];

    self.activity_indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.activity_indicator setColor:[UIColor blackColor]];
    [self.activity_indicator startAnimating];
    self.activity_indicator.center = CGPointMake( 160.f, (240.f-64.f) );
    [waiting_view addSubview:activity_indicator];

    waiting_label = [[UILabel alloc] initWithFrame:CGRectMake( 110.f, (240.f-64.f)+30.f, 100.f, 16.f)];
    waiting_label.font = [UIFont boldSystemFontOfSize:13.f];
    waiting_label.textColor = [UIColor blackColor];
    waiting_label.backgroundColor = [UIColor clearColor];
    waiting_label.textAlignment = UITextAlignmentCenter;
    waiting_label.text = NSLocalizedString(@"Please_Wait_Text", @"");
    [waiting_view addSubview:waiting_label];

    [self.view addSubview:waiting_view];
    [self.view bringSubviewToFront:self.activity_indicator];
    self.waiting_view.hidden = YES;
    self.activity_indicator.hidden = YES;
...
}

- (void) show_waiting_activity
{
    if( self.waiting_view.alpha == 1 && self.waiting_view.hidden == NO )
        return;

    self.waiting_view.hidden = NO;
    self.activity_indicator.hidden = NO;
    [UIView beginAnimations:nil context:NULL]; {
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDuration:.2];        
        self.waiting_view.alpha = 1;
        self.waiting_label.alpha = 1;
        self.activity_indicator.alpha = 1;
    } [UIView commitAnimations];
}



- (void) hide_waiting_activity
{    
    [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDuration:.5];
        self.waiting_view.alpha = 0;
        self.waiting_label.alpha = 0;
        self.activity_indicator.alpha = 0;
    [UIView commitAnimations];
}

我在问自己,这段代码中的 ui 元素在这 0.5 秒内可能会被销毁?

最佳答案

问题不在于您粘贴的代码中 - 问题在于动画开始后它引用的 View (代码中的其他位置)发生了什么。如果动画运行 0.5 秒,所有这些 View 必须在接下来的 0.5 秒内保持事件状态。

因此,您对生命周期不够长的 View 的选择:self、self.waiting_view、self.waiting_label、self.activity_indicator。

关于ios : Nullpointer exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14757158/

相关文章:

javascript - Android OS/iOS/debian linux 中的事件(单击 Enter 键)

ios - 长按而不是简单的轻按

jquery - 使用 JQuery 缩小 css 背景大小

Android 应用程序在开始使用 SQLite 数据库时启动后立即崩溃

java - android studio 媒体播放器 null 对象引用

ios - iOS:降低背景位置更新的频率

ios - Swift:无法关闭模态呈现的 LoginViewController

javascript - 如何为 svg 的整个 "path"元素设置动画?

java - Android:动画自定义 View

android - S9/S9+ 特定 WebView 设备崩溃报告