2009-11-13 75 views
0

我創建了一個應用程序,它將值存儲到數據庫中並檢索存儲的數據。在運行模式下運行應用程序時,一切似乎都正常(值存儲和檢索成功),但是當我在調試模式下運行時,該進程拋出IllegalStateException,至今未找到原因。數據庫問題:如何診斷和解決問題?

以檢索對象記錄的方法如下:

public Recording getRecording(String filename) { 

    Recording recording = null; 
    String where = RECORDING_FILENAME + "='" + filename + "'"; 
    Log.v(TAG, "retrieving recording: filename = " + filename); 

    try { 
     cursor = db.query(DATABASE_TABLE_RECORDINGS, new String[]{RECORDING_FILENAME, RECORDING_TITLE, RECORDING_TAGS, RECORDING_PRIVACY_LEVEL, RECORDING_LOCATION, RECORDING_GEO_TAGS, RECORDING_GEO_TAGGING_ENABLED, RECORDING_TIME_SECONDS, RECORDING_SELECTED_COMMUNITY}, where, null, null, null, null); 

     if (cursor.getCount() > 0) { 
      cursor.moveToFirst(); 
      //String filename = c.getString(0); 
      String title = cursor.getString(1); 
      String tags = cursor.getString(2); 
      int privacyLevel = cursor.getInt(3); 
      String location = cursor.getString(4); 
      String geoTags = cursor.getString(5); 
      int iGeoTaggingEnabled = cursor.getInt(6); 
      String recordingTime = cursor.getString(7); 
      String communityID = cursor.getString(8); 
      cursor.close(); 
      recording = new Recording(filename, title, tags, privacyLevel, location, geoTags, iGeoTaggingEnabled, recordingTime, communityID); 
     } 
    } 
    catch (SQLException e) { 
     String msg = e.getMessage(); 
     Log.w(TAG, msg); 
     recording = null; 
    } 
    return recording; 
} 

並從另一個類稱爲(設置):

private Recording getRecording(String filename) { 
    dbAdapter = dbAdapter.open(); 
    Recording recording = dbAdapter.getRecording(filename); 
    dbAdapter.close(); 
    return recording; 
} 

通過代碼運行雖然上述一切工作正常,但然後我注意到另一個線程中的異常: alt text http://img509.imageshack.us/img509/862/illegalstateexception.jpg

並不知道可能性導致此異常,也無法調試該線程的代碼來診斷原因。

如果有人知道這裏有什麼可能的問題,我會非常感激。

謝謝!

+1

什麼是異常的完整堆棧跟蹤?或者至少,相關的位? – MattC 2009-11-13 18:27:30

回答

2

看起來像cursor.close()是在「if」 - 這是當SQLiteCursor.finalize()將拋出一個IllegalStateException(我谷歌搜索它)。例如,如果某個其他進程/線程沒有足夠的時間提交,您可能會獲得空的記錄集。

總是關閉它,即使它的結果集是空的。

我還建議您按名稱訪問字段,而不是索引,以便將來兼容。並在finally{}塊中同時做close() s。