2012-03-08 103 views
0

我正在用导航控件(编程方式)创建一个带有标签栏应用程序,数据库和分段控件的项目,以便显示来自数据库的信息,并且我得到了带有Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'的SIGABRT在控制台窗口中。NSInvalidArgumentException:[__NSArrayM insertObject:atIndex:]:object can not be nil

TableViewAppDelegate。

#import "SegmentsController.h" 
#import "TableViewAppDelegate.h" 
#import "RootViewController.h" 
#import "AtoZHomePageViewController.h" 
#import "CollectionRecipe.h" 
#import "NSArray+PerformSelector.h" 

@interface TableViewAppDelegate() 

- (NSArray *)segmentViewControllers; 
- (void)firstUserExperience; 
@end 


@implementation TableViewAppDelegate 

@synthesize window; 
@synthesize navigationController; 
@synthesize tabbarController; 
@synthesize recipes; 
@synthesize segmentsController, segmentedControl; 


#pragma mark - 
#pragma mark Application lifecycle 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    NSArray * viewControllers = [self segmentViewControllers]; 
    // UINavigationController * navigationController = [[[UINavigationController alloc] init] autorelease]; 
    self.segmentsController = [[SegmentsController alloc] initWithNavigationController:navigationController viewControllers:viewControllers]; 

    self.segmentedControl = [[UISegmentedControl alloc] initWithItems:[viewControllers arrayByPerformingSelector:@selector(title)]]; 
    self.segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; 

    [self.segmentedControl addTarget:self.segmentsController 
           action:@selector(indexDidChangeForSegmentedControl:) 
        forControlEvents:UIControlEventValueChanged]; 


    databaseName = @"RecipeDatabase.sql"; 

    // Get the path to the documents directory and append the databaseName 
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentPaths objectAtIndex:0]; 
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName]; 

    // Execute the "checkAndCreateDatabase" function 
    [self checkAndCreateDatabase]; 

    // Query the database for all animal records and construct the "animals" array 
    [self readRecipesFromDatabase]; 

    // Configure and show the window 
    [self firstUserExperience]; 
    [window addSubview:[tabbarController view]]; 
    [window addSubview:[navigationController view]]; 
    [window makeKeyAndVisible]; 
    return YES; 
} 

#pragma mark - 
#pragma mark Segment Content 

- (NSArray *)segmentViewControllers { 
    UIViewController * AtoZRecipe  = [[AtoZHomePage alloc] initWithNibName:@"AtoZRecipeController" bundle:nil]; 
    UIViewController * RecipesCollection = [[CollectionRecipe alloc] initWithNibName:@"RecipeCollection" bundle:nil]; 

    NSArray * viewControllers = [NSArray arrayWithObjects:AtoZRecipe, RecipesCollection, nil]; 
    [AtoZRecipe release]; [RecipesCollection release]; 

    return viewControllers; 
} 

- (void)firstUserExperience { 
    self.segmentedControl.selectedSegmentIndex = 0; 
    [self.segmentsController indexDidChangeForSegmentedControl:self.segmentedControl]; 
} 

- (void)applicationWillTerminate:(UIApplication *)application { 
    // Save data if appropriate 
} 
-(void) checkAndCreateDatabase{ 
    // Check if the SQL database has already been saved to the users phone, if not then copy it over 
    BOOL success; 

    // Create a FileManager object, we will use this to check the status 
    // of the database and to copy it over if required 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    // Check if the database has already been created in the users filesystem 
    success = [fileManager fileExistsAtPath:databasePath]; 

    // If the database already exists then return without doing anything 
    if(success) return; 

    // If not then proceed to copy the database from the application to the users filesystem 

    // Get the path to the database in the application package 
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName]; 

    // Copy the database from the package to the users filesystem 
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; 


} 

-(void) readRecipesFromDatabase { 
    // Setup the database object 
    sqlite3 *database; 

    // Init the animals Array 
    recipes = [[NSMutableArray alloc] init]; 

    // Open the database from the users filessytem 
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { 
     // Setup the SQL Statement and compile it for faster access 
     const char *sqlStatement = "select * from recipe order by name"; 
     sqlite3_stmt *compiledStatement; 
     if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { 
      // Loop through the results and add them to the feeds array 
      while(sqlite3_step(compiledStatement) == SQLITE_ROW) { 
       // Read the data from the result row 
       NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; 
       NSString *aAuthor=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,2)]; 
       NSString *aThumbnail=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,3)]; 
       NSString *aPre_time=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,5)]; 
       NSString *aBake_time=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,6)]; 
       NSString *aTota_ltime=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 7)]; 
       NSString *alarge_image=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,8)];      
       NSString *asmall_image=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 9)]; 
       NSString *asummary=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 10)]; 
       NSString *aServe_size=[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement,11)]; 
       // Create a new recipe object with the data from the database 
       AtoZHomePage *recipe=[[AtoZHomePage alloc] initWithName:aName author:aAuthor img_thumbnail:aThumbnail pre_Time:aPre_time bake_Time:aBake_time total_time:aTota_ltime large_Img:alarge_image small_Img:asmall_image summary:asummary serve_size:aServe_size]; 
        // Add the recipe object to the recipes Array 
       [recipes addObject:recipe]; 

       [recipe release]; 
      } 
     } 
     // Release the compiled statement from memory 
     sqlite3_finalize(compiledStatement); 

    } 
    sqlite3_close(database);  
} 



#pragma mark - 
#pragma mark Memory management 

- (void)dealloc { 
    self.segmentedControl = nil; 
    self.segmentsController = nil; 
    [recipes release]; 
    [tabbarController release]; 
    [navigationController release]; 
    [window release]; 
    [super dealloc]; 
} 

@end 

这条线是模糊的NSArray * viewControllers = [NSArray arrayWithObjects:AtoZRecipe, RecipesCollection, nil];,因为我看到最相关的帖子,如果我删除零它会根据我,我有所谓适当的视图控制器缺少定点和。 请你帮我摆脱掉这个bug ...在此先感谢负荷:) :)

+2

'[食谱ADDOBJECT:配方]'此行可能是一次崩溃..在这里添加一个断点。在任何情况下,添加断点在很多地方S的你是男女合校,并检查其功能成功地通过.. – Shubhank 2012-03-08 07:34:23

+3

...或者添加一个异常断点并查看问题的确切位置。断点导航器,加上左下角的符号,添加异常断点。 – jrturton 2012-03-08 08:41:35

+2

在所有方法的开始处放置一个断点,所有方法都会告诉我们您的应用程序停止的位置,这将告诉我们如何解决该问题。考虑到你的代码很长,只要他们看到如此多的代码,并且根本没有线索,人们就会忽略你的问题......只是一个建议,让你的答案更快! – 2012-03-08 08:34:04

回答

3

其实你不分配的内存阵列是你得到NSInvalidArgumentException

试试这个

的原因
NSArray * viewControllers = [NSArray arrayWithArray:[self segmentViewControllers]]; 
+0

我试过上面的代码而不是NSArray * viewControllers = [self segmentViewControllers];还是一样的错误:( – iMeMyself 2012-03-09 10:38:28

相关问题