1. Download our Official Android App: Forums for Android!

Apps Attempt to Finalize Cursor That has not been closed

Discussion in 'Android Development' started by dumbterminal, Jan 1, 2012.

  1. dumbterminal

    dumbterminal Lurker
    Thread Starter
    Rank:
    None
    Points:
    6
    Posts:
    2
    Joined:
    Jan 1, 2012

    Jan 1, 2012
    2
    1
    6
    Hi everyone, I am new to the forum. I ran accross a problem the other day. My application creates a database on the fly and also populates it. The problem is I get a "Attempt to Finalize Cursor That has not been closed" error in logcat which mainly refers to this function.
    Code (Text):
    1.  
    2. public static void populateFromDatabase(){
    3.         /* if the database is empty just throw in the question */
    4.         Cursor c = AppData.db.db.rawQuery("SELECT * FROM question", null);
    5.        
    6.         if (c != null && c.getCount() == 0){
    7.             c.moveToFirst() ;
    8.             int count = 0 ;
    9.             while(!c.isAfterLast()){
    10.                 count++ ;
    11.                 c.moveToNext() ;
    12.             }
    13.             if (count == 0) {
    14.                     // insert some rows
    15.  
    16.             }
    17.         }
    18.         Log.e("Create done", "Create done") ;
    19.         AppData.idList = new ArrayList<Integer>() ;
    20.         AppData.Q = new HashMap<Integer, String>() ;
    21.         AppData.optA = new HashMap<Integer, String>() ;
    22.         AppData.optB = new HashMap<Integer, String>() ;
    23.         AppData.optC = new HashMap<Integer, String>() ;
    24.         AppData.optD = new HashMap<Integer, String>() ;
    25.         AppData.ans = new HashMap<Integer, String>() ;
    26.         AppData.type = new HashMap<Integer, String>() ;
    27.         int qSelected = 0 ;
    28.         int j = 0 ;
    29.         while (qSelected != 6){
    30.             Random rand = new Random() ;
    31.             int index = (rand.nextInt() % 16) ;
    32.             if (index < 0) index *= 1 ;
    33.             index += 1 ;
    34.             if (AppData.Q.containsKey(index))
    35.                 continue ;
    36.            
    37.             c = AppData.db.db.rawQuery("SELECT * FROM question WHERE id=" + index, null) ;
    38.             c.moveToFirst() ;
    39.            
    40.             while (!c.isAfterLast()){
    41.                 Integer id = c.getInt(0) ;
    42.                 String q = c.getString(1) ;
    43.                 String oa = c.getString(2) ;
    44.                 String ob = c.getString(3) ;
    45.                 String oc = c.getString(4) ;
    46.                 String od = c.getString(5) ;
    47.                 String ans = c.getString(6) ;
    48.                 String type = c.getString(7) ;
    49.                
    50.                 AppData.idList.add(id) ;
    51.                 AppData.Q.put(id, q) ;
    52.                 AppData.optA.put(id, oa) ;
    53.                 AppData.optB.put(id, ob) ;
    54.                 AppData.optC.put(id, oc) ;
    55.                 AppData.optD.put(id, od) ;
    56.                 AppData.ans.put(id, ans) ;
    57.                 AppData.type.put(id, type) ;
    58.                
    59.                 qSelected++ ;
    60.                 j++ ;
    61.                 //Toast.makeText(null, q + oa + ob + oc + od + ans, Toast.LENGTH_LONG) ;
    62.                 Log.e("qqq", id + " " + q + oa + ob + oc + od + ans) ;
    63.                 c.moveToNext() ;
    64.                
    65.             }
    66.            
    67.            
    68.         }
    69.         AppData.qCount = 6 ;
    70.         c.close() ;
    71.  
    72.        
    73.     }
    74.  
    My question is I am closing the cursor in all occasions so why this error ? Also note that i call this function only once in the program and the logcat error points to this, can some1 point out what I am doing wrong here.
    Thanx in advancce
     

    Advertisement

  2. alostpacket

    alostpacket Over Macho Grande?
    Rank:
    None
    Points:
    513
    Posts:
    7,972
    Joined:
    Nov 29, 2009

    Nov 29, 2009
    7,972
    3,603
    513
    Android App Developer
    NY
    it looks like you have two cursors, both named c, one inside a while-loop that gets changed for each iteration of the while loop and has no calls to c.close().

    I'd suspect this is the root of the problem.

    You may want to name your second cursor to c2 or something and be sure to close it after each and every time you set it to a different rawQuery.

    edit, also welcome to the forums! :)
     
    dumbterminal likes this.
  3. dumbterminal

    dumbterminal Lurker
    Thread Starter
    Rank:
    None
    Points:
    6
    Posts:
    2
    Joined:
    Jan 1, 2012

    Jan 1, 2012
    2
    1
    6
    yeah u got it r8, got rid of the problem, thanks a lot.
     
    alostpacket likes this.

Share This Page

Loading...