好日子,我有一個小部件應用程序上我的sqlite數據庫的這個問題,即使當我明確地調用它時,它永遠不會關閉。我得到這個錯誤logcat中光標和數據庫沒有關閉android
02-26 00:49:42.070: E/Database(18049): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
,由於某種原因,當我得到一個遊標並調用getCount將()方法,它總是與最後的值還增加而遞增。例如..如果我得到遊標和getCount()方法從列中返回值5,我以後刪除小部件。當我再次添加小部件並查詢不同的列時,我知道它的值爲6,我反而從光標獲得11作爲返回值。 getCount()方法。它像遊標和數據庫從不關閉。任何想法可能是錯的..
方法:
private void setTag(String tag){
WeatherImageDB weatherimagedb = new WeatherImageDB(this); // get logcat error on this line
weatherimagedb.open();
ArrayList<String> image_list = new ArrayList<String>();
image_list.clear();
if(weatherimagedb.open() != null){
Cursor cursor = weatherimagedb.retrieveTag(tag);
Log.d(TAG, "size is " + cursor.getCount()); // this always gets incremented with also the last value added
if(cursor.moveToFirst()){
do{
image_list.add(cursor.getString(cursor.getColumnIndex(tag)));
//System.out.println("imagelisted!!");
}
while(cursor.moveToNext());
}
//do other stuffs here with arraylist and then clear
}
image_list.clear();
cursor.close();
weatherimagedb.close();
}
和代碼數據庫類:
private final Context mcontext;
private DBHelper dbhelper;
private SQLiteDatabase db;
public WeatherImageDB(Context context){
mcontext = context;
dbhelper = new DBHelper(mcontext);
}
public class DBHelper extends SQLiteOpenHelper {
DBHelper(Context context){
super(context, IMAGE_DB, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS imagetags");
onCreate(db);
}
}
public WeatherImageDB open() throws SQLException {
try{
db = dbhelper.getWritableDatabase();
}catch(SQLException e){
db = dbhelper.getReadableDatabase();
}
return this;
}
public void close(){
Log.d(TAG, "calling close on Database Object here");
if(dbhelper != null){
dbhelper.close();
db.close();
}
}
public long tagImage(String pathname, ContentValues val){
return db.insert(DATABASE_TABLE, null, val);
}
public boolean deleteTag(String entry){
return db.delete(DATABASE_TABLE, null, null) >0;
}
public Cursor retrieveTag(String tag){
//open();
String[] condition = {tag};
Cursor cursor = db.query(DATABASE_TABLE, condition, null, null, null, null, null);
return cursor;
}
}
任何人有一個想法嗎?它不會崩潰我的應用程序,但我想刪除所有的錯誤..我希望我解釋清楚我的問題..謝謝 – irobotxxx 2012-02-26 02:44:13