【问题标题】:'NSException' Error, Crashes after pressing rectangle button - Cocoa Touch'NSException' 错误,按下矩形按钮后崩溃 - Cocoa Touch
【发布时间】:2011-04-23 17:46:55
【问题描述】:

我制作了一个矩形按钮,当用户点击它时,他们会转到他们的 iPod。

在调试时按下按钮后出现此错误。我已经彻底检查了 iPod 代码,一切似乎都是正确的。

这是在抛出NSException 的实例后调用的调试器的错误:

2011-04-23 18:42:11.580 iApp[1197:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x01369be9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x0115e5c2 objc_exception_throw + 47
    2   CoreFoundation                      0x01322628 +[NSException raise:format:arguments:] + 136
    3   CoreFoundation                      0x0132259a +[NSException raise:format:] + 58
    4   Foundation                          0x00065b12 -[NSURL(NSURL) initFileURLWithPath:] + 90
    5   iApp                             0x000056f3 -[MainViewController setupApplicationAudio] + 148
    6   iApp                             0x00005af0 -[MainViewController viewDidLoad] + 78
    7   UIKit                               0x0037765e -[UIViewController view] + 179
    8   UIKit                               0x00379012 -[UIViewController viewControllerForRotation] + 63
    9   UIKit                               0x00374f76 -[UIViewController _visibleView] + 90
    10  UIKit                               0x0060ea97 -[UIClientRotationContext initWithClient:toOrientation:duration:andWindow:] + 354
    11  UIKit                               0x002f0ba8 -[UIWindow _setRotatableClient:toOrientation:updateStatusBar:duration:force:] + 954
    12  UIKit                               0x00570948 -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:] + 1053
    13  UIKit                               0x0037b982 -[UIViewController presentModalViewController:withTransition:] + 3151
    14  iApp                           0x00002330 -[MainerViewController goImport:] + 153
    15  UIKit                               0x002c9a6e -[UIApplication sendAction:to:from:forEvent:] + 119
    16  UIKit                               0x003581b5 -[UIControl sendAction:to:forEvent:] + 67
    17  UIKit                               0x0035a647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
    18  UIKit                               0x003591f4 -[UIControl touchesEnded:withEvent:] + 458
    19  UIKit                               0x002ee0d1 -[UIWindow _sendTouchesForEvent:] + 567
    20  UIKit                               0x002cf37a -[UIApplication sendEvent:] + 447
    21  UIKit                               0x002d4732 _UIApplicationHandleEvent + 7576
    22  GraphicsServices                    0x01b80a36 PurpleEventCallback + 1550
    23  CoreFoundation                      0x0134b064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    24  CoreFoundation                      0x012ab6f7 __CFRunLoopDoSource1 + 215
    25  CoreFoundation                      0x012a8983 __CFRunLoopRun + 979
    26  CoreFoundation                      0x012a8240 CFRunLoopRunSpecific + 208
    27  CoreFoundation                      0x012a8161 CFRunLoopRunInMode + 97
    28  GraphicsServices                    0x01b7f268 GSEventRunModal + 217
    29  GraphicsServices                    0x01b7f32d GSEventRun + 115
    30  UIKit                               0x002d842e UIApplicationMain + 1160
    31  iApp                            0x00001b34 main + 102
    32  iApp                             0x00001ac5 start + 53
    33  ???                                 0x00000001 0x0 + 1
)
terminate

【问题讨论】:

  • 你能把你的容易崩溃的代码吗?
  • 在这种情况下,发布导致错误的代码,并且只发布异常没有完整的跟踪可能会更有帮助。错误消息表明您正在将 nil 参数传递给 -[NSURL initFileURLWithPath:];你调查过吗?
  • 好吧,这是 initfilewithurlwithpath 的唯一实例 ... NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath]; self.soundFileURL = newURL; [新网址发布];
  • 耶修好了..我没有包含声音文件,我很傻!
  • 所以soundFilePath 至少在某些时候显然是nil。弄清楚什么时候以及为什么你已经解决了你的问题。

标签: iphone objective-c cocoa-touch xcode nsexception


【解决方案1】:

如果您查看崩溃报告的第一行,它表明您正在将一个 nil 字符串传递给 NSURL 的 initFileURLWithPath 方法:

'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'

来自 Apple 文档。

为此参数传递 nil 会产生异常。

所以在发送到 initFileURLWithPath 方法之前一定要检查你的 NSString,

NSURL* targetFileURL = nil ;
if(targetFilePath != nil)
{
       targetFileURL = [[NSURL alloc] initFileURLWithPath:targetFilePath];
}

【讨论】:

  • 你不想重新声明targetFileURL;在第 4 行中去掉 NSURL *
  • 谢谢,只是我忘记将 sound.caf 添加到项目中 - 仅将其添加到文件夹中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多