[NSURL initFileURLWithPath:]:尝试加载视图时的nil字符串参数

时间:2012-01-11 15:20:33

标签: ios uiviewcontroller

当我的视图尝试加载时,应用程序崩溃,我得到这个堆栈:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'

这是我的代码,我真的不知道发生此次崩溃时的位置:

#import "Itineraire.h"
#import "SBMapView.h"
#import "SBRouteAnnotation.h"
#import "City.h"
#import "UICGRoutes.h"
#import "SBCheckPointViewController.h"
//#import "SBRouteDetailView.h"

@interface Itineraire(Private)
-(void)customInitialization;
@end

@implementation Itineraire(Private)

-(void)customInitialization
{
    // do the initialization of class variables here..

    mDirections          = [UICGDirections sharedDirections];
    mDirections.delegate = self;
}

@end

@implementation Itineraire

@synthesize map             = mMap;
@synthesize startPoint      = mStartPoint;
@synthesize endPoint        = mEndPoint;
@synthesize loadBtn     = mLoadBtn;
@synthesize annotationArray = mAnnotationArray;
@synthesize travelMode      = mTravelMode;
@synthesize destination;
@synthesize routes;
@synthesize mAnnotations;
@synthesize mRouteArray;
@synthesize mRouteDetail;

//Invoked when the class is instantiated in XIB
-(id)initWithCoder:(NSCoder*)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if( self)
    {
        [self customInitialization];
    }
    return self;
}

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        [self customInitialization];
    }
    return self;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Google Maps";
    self.map = [[SBMapView alloc] initWithFrame:CGRectMake(0, 49, self.view.frame.size.width, self.view.frame.size.height)];
    //self.map = [[SBMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 381)];

    [self.view addSubview:mMap];

    self.view.backgroundColor = [UIColor blackColor];
    self.annotationArray = [[NSMutableArray alloc]init];
    self.routes          = [[UICGRoutes alloc]init];


    if (mDirections.isInitialized) {
        [self updateRoute];
    }
}
/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

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

- (void)viewDidUnload {
    [super viewDidUnload];

}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];

}
- (void)viewWillDisappear:(BOOL)animated; 
{
    [super viewWillDisappear:YES];

}

#pragma mark -
#pragma mark Instance Methods

- (void)updateRoute
{   /*
     [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
     UICGDirectionsOptions *options = [[[UICGDirectionsOptions alloc] init] autorelease];
     options.travelMode = mTravelMode;
     City *mFirstCity = [[[City alloc]init] autorelease];
     mFirstCity.mCityName = @"Paris";//mStartPoint;
     //[mDirections loadWithStartPoint:mFirstCity.mCityName endPoint:destination options:options];
     //added
     NSMutableArray * DestinationCityArray = [[NSMutableArray alloc]init];
     [DestinationCityArray addObject:@"Berlin"];
     [mDirections loadWithStartPoint:mFirstCity.mCityName endPoint:destination options:options];
     */
    //
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    UICGDirectionsOptions *options = [[UICGDirectionsOptions alloc] init] ;
    options.travelMode = mTravelMode;
    City *mFirstCity = [[City alloc]init];
    mFirstCity.mCityName = @"Amsterdam";//mStartPoint;
    NSMutableArray *dest=[[NSMutableArray alloc]init];
    [dest addObject:@"Berlin"];
    [mDirections loadWithStartPoint:mFirstCity.mCityName endPoint:dest options:options];


}

-(void)loadRouteAnnotations
{
    self.mRouteArray = [mDirections routeArray];
    NSLog(@"mRouteArray %@",mRouteArray);
    self.mAnnotations = [[NSMutableArray alloc]init];
    for (int idx = 0; idx < [mRouteArray count]; idx++) {
        NSArray *_routeWayPoints1 = [[mRouteArray objectAtIndex:idx] wayPoints];
        NSArray *mPlacetitles = [[mRouteArray objectAtIndex:idx] mPlaceTitle]; 
        self.annotationArray = [NSMutableArray arrayWithCapacity:[_routeWayPoints1 count]-2];

        mLoadBtn.title = @"OFF";
        mLoadBtn.target = self;
        mLoadBtn.action = @selector(removeRouteAnnotations);

        for(int idx = 0; idx < [_routeWayPoints1 count]-1; idx++)
        {

            mBetweenAnnotation = [[SBRouteAnnotation alloc] initWithCoordinate:[[_routeWayPoints1 objectAtIndex:idx]coordinate]
                                                                          title:[mPlacetitles objectAtIndex:idx]
                                                                 annotationType:SBRouteAnnotationTypeWayPoint];
            [self.annotationArray addObject:mBetweenAnnotation];
        }
        [mAnnotations addObject:mAnnotationArray];
        [self.map.mapView addAnnotations:[mAnnotations objectAtIndex:idx]];
        NSLog(@"map %@",mMap);

    }   
}

-(void)showCheckpoints
{
    SBCheckPointViewController *_Controller = [[SBCheckPointViewController alloc]initWithNibName:@"SBCheckPoints" bundle:nil];
    [self.navigationController pushViewController:_Controller animated:YES];
    NSMutableArray *arr = [[mDirections checkPoint] mPlaceTitle];
    _Controller.mCheckPoints = arr ;


}



//
-(void)removeRouteAnnotations
{
    NSMutableArray *mTempAnnotation;// = [mAnnotations retain];
    for (int idx = 0; idx < [mTempAnnotation count]; idx++) {
        [mMap.mapView removeAnnotations:[mTempAnnotation objectAtIndex:idx] ];
    }   
    mLoadBtn.title = @"ON";
    mLoadBtn.target = self;
    mLoadBtn.action = @selector(loadRouteAnnotations);
}


#pragma mark <UICGDirectionsDelegate> Methods

- (void)directionsDidFinishInitialize:(UICGDirections *)directions {
    [self updateRoute];
}

- (void)directions:(UICGDirections *)directions didFailInitializeWithError:(NSError *)error {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Map Directions" message:[error localizedFailureReason] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alertView show];
}

- (void)directionsDidUpdateDirections:(UICGDirections *)indirections {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

    UICGPolyline *polyline = [indirections polyline];
    NSArray *routePoints = [polyline routePoints];

    [mMap loadRoutes:routePoints]; // Loads route by getting the array of all coordinates in the route.

    UIToolbar *tools = [[UIToolbar alloc]
                        initWithFrame:CGRectMake(0.0f, 0.0f, 103.0f, 44.01f)]; // 44.01 shifts it up 1px for some reason
    tools.clearsContextBeforeDrawing = NO;
    tools.clipsToBounds = NO;
    tools.tintColor = [UIColor colorWithWhite:0.305f alpha:0.0f]; // closest I could get by eye to black, translucent style.
    // anyone know how to get it perfect?
    tools.barStyle = -1; // clear background
    NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2];

    // Create a standard Load button.

    self.loadBtn = [[UIBarButtonItem alloc]initWithTitle:@"ON" 
                                                   style:UIBarButtonItemStyleBordered 
                                                  target:self 
                                                  action:@selector(loadRouteAnnotations)];

    [buttons addObject:mLoadBtn];

    // Add Go button.
    UIBarButtonItem *mGoBtn = [[UIBarButtonItem alloc] initWithTitle:@"Go" 
                                                               style:UIBarButtonItemStyleBordered 
                                                              target:self 
                                                              action:@selector(showCheckpoints)];
    [buttons addObject:mGoBtn];

    // Add buttons to toolbar and toolbar to nav bar.
    [tools setItems:buttons animated:NO];
    UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
    self.navigationItem.rightBarButtonItem = twoButtons;

    //Add annotations of different colors based on initial and final places.
    SBRouteAnnotation *startAnnotation = [[SBRouteAnnotation alloc] initWithCoordinate:[[routePoints objectAtIndex:0] coordinate]
                                                                                  title:mStartPoint
                                                                         annotationType:SBRouteAnnotationTypeStart];
    SBRouteAnnotation *endAnnotation = [[SBRouteAnnotation alloc] initWithCoordinate:[[routePoints lastObject] coordinate]
                                                                                title:mEndPoint
                                                                       annotationType:SBRouteAnnotationTypeEnd];


    [mMap.mapView addAnnotations:[NSArray arrayWithObjects:startAnnotation, endAnnotation,nil]];
}

- (void)directions:(UICGDirections *)directions didFailWithMessage:(NSString *)message {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Map Directions" message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alertView show];
}

#pragma mark -

@end

我试图将我的代码放在一个新项目(空应用程序)上并且工作正常,我不理解该错误及其可能的原因。

修改

我试图通过消除来跟踪问题,当我删除此方法时,我的代码工作正常:

//Invoked when the class is instantiated in XIB

-(id)initWithCoder:(NSCoder*)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if( self)
    {
        [self customInitialization];
    }
    return self;
}

然而,当我跑步时,我的应用程序不会显示两点之间的路线(应用程序的目的)。因此,这个方法对整个班级来说似乎很重要,因为没有它,我无法获得路线,而在另一侧,当它在那里时,应用程序崩溃了。我该如何处理这个矛盾?

1 个答案:

答案 0 :(得分:2)

@Vince正在让你走上正轨。由于问题在于框架调用NSURL方法(在本例中为Google Maps),因此您需要调试传递给框架的内容。你有框架的来源吗?您可以在loadWithStartPoint:endPoint:options:]中设置断点以查看正在发生的情况。

我注意到的一件事是,当我认为它需要NSMutableArray时,您正在传递endPoint作为NSString参数。