2013-05-15 42 views
3

我讀從Android的Google日曆的一些數據,有時我從用戶喜歡有陌生的崩潰報告:無法從CursorWindow讀取第384行,第47行。確保光標被初始化正確

java.lang.IllegalStateException: Couldn't read row 384, col 47 from CursorWindow. 
Make sure the Cursor is initialized correctly before accessing data from it. 

我的代碼是在這裏(粗體行,其中的應用程序崩潰):

 Cursor eventCursor = contentResolver.query 
      (builder.build(), 
      null, 
      CalendarContract.Instances.CALENDAR_ID + " IN (" + ids + ")", 
      null, 
      null); 

     if (eventCursor == null) 
      return true; 

     while (eventCursor.moveToNext()) { //this line causecrash 
      ... do something... 
     } 

爲什麼會發生這種情況?它不能被模擬。它永遠不會發生在我身上,我無法理解理由和錯誤信息。

回答

0

在迭代開始時,使用eventCursor.moveToFirst()移動到第一行。你可以使用這樣的事情:

if (eventCursor != null) { 

    //Start from beginning 
    eventCursor.moveToFirst(); 

    // Loop over rows 
    while (eventCursor.moveToNext()) { 

     // Do Somehing here 
    } 
} 

您還可以檢查光標是否具有行或不使用eventCursor.getCount()

相關問題