2012-02-26 34 views
0

好日子,我有一個小部件應用程序上我的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; 
    } 

} 
+0

任何人有一個想法嗎?它不會崩潰我的應用程序,但我想刪除所有的錯誤..我希望我解釋清楚我的問題..謝謝 – irobotxxx 2012-02-26 02:44:13

回答

1

,你宣佈將裏面如果只循環光標的範圍。 ...我認爲可能會有一些其他全局對象與「光標」同名...

if(weatherimagedb.open() != null){ 
    Cursor cursor = weatherimagedb.retrieveTag(tag); 
    //some stuff--> make sure there are no return statements in this part. 
     //If any return statement exists close the cursor and then execute return 

    //it should be closed here 
    cursor.close(); 
} 
//You are closing outside if loop which is holding reference of some other cursor 
cursor.close(); 
+0

抱歉,遲到的答覆。我關閉了if語句中的遊標,但它仍然給麻煩。全球對象對此意味着什麼。你的意思是在應用程序中還是在活動/服務中? – irobotxxx 2012-02-26 17:34:49

+0

在示例代碼中,您給遊標的指針正在被關閉到if循環的旁邊......但是,如果循環中指定了遊標,這表示您在if循環之外關閉的遊標不是正在聲明的遊標裏面如果循環... – 2012-02-27 04:29:07

+0

哎呀看到你的觀點,從我的角度監督。感謝您指出了這一點。 – irobotxxx 2012-02-27 14:07:53