2011-02-02 34 views
2
02-02 14:31:34.048: WARN/SQLiteCompiledSql(359): Releasing statement in a finalizer. Please ensure that you explicitly call close() on your cursor: SELECT * FROM 
02-02 14:31:34.048: WARN/SQLiteCompiledSql(359): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here 
02-02 14:31:34.129: ERROR/Database(359): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here 
02-02 14:31:34.129: ERROR/Database(359):  at 

如何避免這種異常?請幫助如何避免db不關閉和遊標異常

我的代碼如下:

**DataSQLHelper .class** 

public class DataSQLHelper extends SQLiteOpenHelper { 
private static final String DATABASE_NAME = "test.db"; 
private static final int DATABASE_VERSION = 1; 

    public DataSQLHelper (Context context) { 
super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 



@Override 
public void onCreate(SQLiteDatabase db) { 
    String sql = "create table " + TABLE + "(" + BaseColumns._ID 
    + " integer primary key autoincrement, " + ID + " text, " 
    + PASSWORD + " text, " + ACTIVE + " text, " + STATUS 
    + " text);"; 
    db.execSQL(sql); 

} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
if (oldVersion >= newVersion) 
    return; 

String sql = null; 
if (oldVersion == 1) 
    sql = "alter table " + TABLE + " add note text;"; 
if (oldVersion == 2) 
    sql = ""; 


if (sql != null) 
    db.execSQL(sql); 
} 

     @Override 
    public synchronized void close() { 
        super.close(); 


    } 

} 


    ***Test_Java .java*** 

    public class Test_Java extends Activity { 
    DataSQLHelper helData; 
SQLiteDatabase db; 
Cursor cursor; 


@Override 
public void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    try {  

     helData= new DataSQLHelper(this); 
    cursor = getData(); 
    startManagingCursor(cursor); 

    setContentView(R.layout.view); 


} catch (Exception ex) { 

    } 

    } // onCreate Ends 



private Cursor getData() { 
try { 
    db = helData.getReadableDatabase(); 
    cursor = db.query(DataSQLHelper.TABLE, null, null, 
     null, null, null, null); 
    startManagingCursor(cursor); 
    return cursor; 
} catch (Exception ex) { 
    System.out.println("Exception Occured : " + ex.toString()); 
    return null; 
} 

} 

@Override 
protected void onDestroy() { 
    System.out.println("onDestroy"); 
    super.onDestroy(); 
    if (db!=null){ 
        db.close(); 
       } 
    if (cursor!=null){ 
        cursor.close(); 
    } 

    if (helData!=null){ 
     helData.close(); 
    } 

} 
} 

回答

0

我解決了這個例外。我打電話

db = eventsData.getReadableDatabase(); 

兩次這就是爲什麼拋出異常

4

你應該扭轉你的onDestroy()方法中的密切語句。 首先關閉遊標,那麼DB:

if (cursor!=null){ 
    cursor.close(); 
} 
if (db!=null){ 
    db.close(); 
} 

你基本上需要扭轉創建/打開數據庫和光標的順序。

另請注意,示例中的db/cursor在onCreate()中打開,這是來自系統的回調。您可能希望在離開該方法之前關閉遊標/ db。您可以在您的 應用程序中自由緩存DataSQLHelper。

在我的應用程序Zwitscher中,我已經將整個db/cursor處理與SQLHelper類的方法進行處理,以便上面的層不必關心它。請參閱其TweetDB課程。

+0

我給這樣的頂尖舞者..但仍然得到同樣的異常..而行是方法的getData()內; db = helData.getReadableDatabase(); &cursor = db.query(DataSQLHelper.TABLE,null,null, null,null,null,null); – MorningGlory 2011-02-02 09:39:05