0

在 viewDidLoad 我只有 2 个字符串:

MKMapView *mapViewMy = [[MKMapView alloc]init];
[self.view addSubview:mapViewMy];

该应用程序在“[self.view addSubview:mapViewMy]”上崩溃了。我用的是iOS7。它在设备和模拟器上都崩溃。当我使用模拟器时,我有崩溃报告:

2013-11-06 13:34:15.948 MyBex JAPanel[36286:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(
    0   CoreFoundation                      0x0216c5e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x01eef8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x0211ebcc -[__NSArrayM insertObject:atIndex:] + 844
    3   CoreFoundation                      0x0211e870 -[__NSArrayM addObject:] + 64
    4   UIKit                               0x01190b67 PushNextClassForSettingIMP + 358
    5   UIKit                               0x0118905e TaggingAppearanceObjectSetterIMP + 43
    6   UIKit                               0x00d6d8de -[UIViewController _setUpLayoutGuideConstraintIfNecessaryAtTop:] + 424
    7   UIKit                               0x00d6c1c3 -[UIViewController topLayoutGuide] + 140
    8   MapKit                              0x023d16a8 -[MKMapView updateLayoutGuides] + 82
    9   MapKit                              0x023d17e7 -[MKMapView didMoveToSuperview] + 68
    10  UIKit                               0x00cba636 __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 472
    11  UIKit                               0x00cba388 -[UIView(Hierarchy) _postMovedFromSuperview:] + 260
    12  UIKit                               0x00cc54c1 -[UIView(Internal) _addSubview:positioned:relativeTo:] + 1847
    13  UIKit                               0x00cb89b1 -[UIView(Hierarchy) addSubview:] + 56
    14  UIKit                               0x01189077 TaggingAppearanceObjectSetterIMP + 68
    15  MyBex JAPanel                       0x0001180e -[MapViewController configureView] + 142
    16  MyBex JAPanel                       0x00011778 -[MapViewController viewDidLoad] + 88
    17  UIKit                               0x00d71318 -[UIViewController loadViewIfRequired] + 696
    18  UIKit                               0x00d96b15 -[UINavigationController _layoutViewController:] + 39
    19  UIKit                               0x00d9702b -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 235
    20  UIKit                               0x00d97123 -[UINavigationController _startTransition:fromViewController:toViewController:] + 78
    21  UIKit                               0x00d9809c -[UINavigationController _startDeferredTransitionIfNeeded:] + 645
    22  UIKit                               0x00d98cb9 -[UINavigationController __viewWillLayoutSubviews] + 57
    23  UIKit                               0x00ed2181 -[UILayoutContainerView layoutSubviews] + 213
    24  UIKit                               0x00cc8267 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 355
    25  libobjc.A.dylib                     0x01f0181f -[NSObject performSelector:withObject:] + 70
    26  QuartzCore                          0x005c82ea -[CALayer layoutSublayers] + 148
    27  QuartzCore                          0x005bc0d4 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    28  QuartzCore                          0x005bbf40 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26
    29  QuartzCore                          0x00523ae6 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 294
    30  QuartzCore                          0x00524e71 _ZN2CA11Transaction6commitEv + 393
    31  QuartzCore                          0x005e1430 +[CATransaction flush] + 52
    32  UIKit                               0x00c79dc9 _afterCACommitHandler + 131
    33  CoreFoundation                      0x021344ce __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30
    34  CoreFoundation                      0x0213441f __CFRunLoopDoObservers + 399
    35  CoreFoundation                      0x02112344 __CFRunLoopRun + 1076
    36  CoreFoundation                      0x02111ac3 CFRunLoopRunSpecific + 467
    37  CoreFoundation                      0x021118db CFRunLoopRunInMode + 123
    38  GraphicsServices                    0x027f29e2 GSEventRunModal + 192
    39  GraphicsServices                    0x027f2809 GSEventRun + 104
    40  UIKit                               0x00c5dd3b UIApplicationMain + 1225
    41  MyBex JAPanel                       0x0003bc0d main + 141
    42  libdyld.dylib                       0x03423725 start + 0
    43  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

我查了一下,两者mapViewMy and self.view都不为零。

4

2 回答 2

0

在我们的viewDidLoad中,您的对象似乎缺少引用

  1. 确保您已添加MapKit框架
  2. 在您的 .h 文件中导入 Map Kit & Delegate

试试这个代码:

MKMapView *mapViewMy = [[MKMapView alloc] initWithFrame:self.view.bounds]; 
mapViewMy.delegate=self; 

[self.view addSubview:mapViewMy]; 

您需要添加地图工具包委托方法。:)

于 2013-11-06T10:52:18.550 回答
0

.h文件中

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
//import MKMapkit framwork

@interface mapSampleViewController : UIViewController {
    <MKMapViewDelegate>//mapview delegate
{
        MKMapView *mapView;

}

.m文件中

在viewdidload中添加此代码

 mapView= [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

 mapView.delegate = self;

 [self.view addSubview:mapView];
于 2013-11-06T11:11:35.630 回答