2013-02-26 4 views
0

내 응용 프로그램을 실행하려고하면이 오류가 발생하고 무엇이 잘못 될지 알 수 없습니다. Cursor 예외를 벗어났습니다.안드로이드 애플 리케이션에 대한 CursorIndexOutOfBoundsException

스택 추적은 :

02-26 12:33:56.870: D/AndroidRuntime(1012): Shutting down VM 
    02-26 12:33:56.870: W/dalvikvm(1012): threadid=1: thread exiting with uncaught exception (group=0x41452930) 
    02-26 12:33:56.870: E/AndroidRuntime(1012): FATAL EXCEPTION: main 
    02-26 12:33:56.870: E/AndroidRuntime(1012): java.lang.RuntimeException: Unable to create application com.example.myassistantcoach.AnotherHelper: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0 

여기

private void readBowlingScoresFromDB() { 
     // TODO Auto-generated method stub 
     allBowlingScores = new ArrayList<BowlingScores>(); 
     Cursor bowlingScoresCursor; 
     bowlingScoresCursor = bowlingScoresDB.query(BOWLING_SCORES_TABLE, 
       new String[] { RECORD_ID, DATE, GAME1, GAME2, GAME3 }, null, 
       null, null, null, DATE); 
     bowlingScoresCursor.moveToFirst(); 
     BowlingScores tempBS; 
     if (!bowlingScoresCursor.isAfterLast()) { 
      do { 
       long id = bowlingScoresCursor.getLong(0); 
       long dateEpoch = bowlingScoresCursor.getLong(1); 
       int game1 = bowlingScoresCursor.getInt(2); 
       int game2 = bowlingScoresCursor.getInt(3); 
       int game3 = bowlingScoresCursor.getInt(4); 
       tempBS = new BowlingScores(id, dateEpoch, game1, game2, game3); 
       allBowlingScores.add(tempBS); 
      } while (bowlingScoresCursor.moveToNext()); 
      bowlingScoresCursor.close(); 
     } 
    } 

    public void addBowlingScores(BowlingScores bowlingScores) { 
     assert bowlingScores != null; 
     ContentValues cv = new ContentValues(); 
     cv.put(BowlingScoresDatabaseHelper.DATE, bowlingScores.getDateEpoch()); // USE 
                       // EPOCH 
     cv.put(BowlingScoresDatabaseHelper.GAME1, bowlingScores.getGame1()); 
     cv.put(BowlingScoresDatabaseHelper.GAME2, bowlingScores.getGame2()); 
     cv.put(BowlingScoresDatabaseHelper.GAME3, bowlingScores.getGame3()); 
     Log.d("Bowling Database", "Before Inserting a record" + bowlingScores); 
     long idPassedBack = bowlingScoresDB.insert(
       BowlingScoresDatabaseHelper.BOWLING_SCORES_TABLE, null, cv); 
     bowlingScores.setId(idPassedBack); 
     allBowlingScores.add(bowlingScores); 
    } 

답변

0

귀하의 커서가 readBowlingScoresFromDB를 통해 첫 번째 패스()에 비어있는 내 코드입니다. if 문을 사용하여 do-while 루프 대신 for 루프를 사용하는 것이 좋습니다. 첫 번째 반복에서 코드를 실행하지 않고 커서가 비었을 때 건너 뜁니다.

for (bowlingScoresCursor.moveToFirst(); !bowlingScoresCursor.isAfterLast(); bowlingScoresCursor.moveToNext()) { 

      long id = bowlingScoresCursor.getLong(0); 
      long dateEpoch = bowlingScoresCursor.getLong(1); 
      int game1 = bowlingScoresCursor.getInt(2); 
      int game2 = bowlingScoresCursor.getInt(3); 
      int game3 = bowlingScoresCursor.getInt(4); 
      tempBS = new BowlingScores(id, dateEpoch, game1, game2, game3); 
      allBowlingScores.add(tempBS); 
} 

bowlingScoresCursor.close(); 
관련 문제