Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development

Application Development Dev Lounge for the Coder Folks



Reply
 
LinkBack Thread Tools
Old January 1st, 2012, 02:16 AM   #1 (permalink)
New Member
 
Join Date: Jan 2012
Posts: 2
 
Device(s):
Thanks: 1
Thanked 1 Time in 1 Post
Exclamation Attempt to Finalize Cursor That has not been closed

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:
public static void populateFromDatabase(){
        /* if the database is empty just throw in the question */
        Cursor c = AppData.db.db.rawQuery("SELECT * FROM question", null);
        
        if (c != null && c.getCount() == 0){
            c.moveToFirst() ;
            int count = 0 ;
            while(!c.isAfterLast()){
                count++ ;
                c.moveToNext() ;
            }
            if (count == 0) {
                    // insert some rows

            }
        }
        Log.e("Create done", "Create done") ;
        AppData.idList = new ArrayList<Integer>() ;
        AppData.Q = new HashMap<Integer, String>() ;
        AppData.optA = new HashMap<Integer, String>() ;
        AppData.optB = new HashMap<Integer, String>() ;
        AppData.optC = new HashMap<Integer, String>() ;
        AppData.optD = new HashMap<Integer, String>() ;
        AppData.ans = new HashMap<Integer, String>() ;
        AppData.type = new HashMap<Integer, String>() ;
        int qSelected = 0 ;
        int j = 0 ;
        while (qSelected != 6){
            Random rand = new Random() ;
            int index = (rand.nextInt() % 16) ;
            if (index < 0) index *= 1 ;
            index += 1 ;
            if (AppData.Q.containsKey(index))
                continue ;
            
            c = AppData.db.db.rawQuery("SELECT * FROM question WHERE id=" + index, null) ;
            c.moveToFirst() ;
            
            while (!c.isAfterLast()){
                Integer id = c.getInt(0) ;
                String q = c.getString(1) ;
                String oa = c.getString(2) ;
                String ob = c.getString(3) ;
                String oc = c.getString(4) ;
                String od = c.getString(5) ;
                String ans = c.getString(6) ;
                String type = c.getString(7) ;
                
                AppData.idList.add(id) ;
                AppData.Q.put(id, q) ;
                AppData.optA.put(id, oa) ;
                AppData.optB.put(id, ob) ;
                AppData.optC.put(id, oc) ;
                AppData.optD.put(id, od) ;
                AppData.ans.put(id, ans) ;
                AppData.type.put(id, type) ;
                
                qSelected++ ;
                j++ ;
                //Toast.makeText(null, q + oa + ob + oc + od + ans, Toast.LENGTH_LONG) ;
                Log.e("qqq", id + " " + q + oa + ob + oc + od + ans) ;
                c.moveToNext() ;
                
            }
            
            
        }
        AppData.qCount = 6 ;
        c.close() ;

        
    }
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

dumbterminal is offline  
Reply With Quote
Sponsors
Old January 4th, 2012, 11:32 AM   #2 (permalink)
Over Macho Grande?
 
alostpacket's Avatar
 
Join Date: Nov 2009
Location: NY
Posts: 7,090
 
Device(s): GalaxyNexus(LTE), NexusOne, OG Droid, GalaxyTab 10.1(LTE), Eris, Logitech Revue (fishtank)
Thanks: 4,164
Thanked 3,126 Times in 1,292 Posts
Default

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!
alostpacket is offline  
Last edited by alostpacket; January 4th, 2012 at 11:35 AM.
Reply With Quote
The Following User Says Thank You to alostpacket For This Useful Post:
dumbterminal (January 4th, 2012)
Old January 4th, 2012, 01:12 PM   #3 (permalink)
New Member
 
Join Date: Jan 2012
Posts: 2
 
Device(s):
Thanks: 1
Thanked 1 Time in 1 Post
Default

yeah u got it r8, got rid of the problem, thanks a lot.
dumbterminal is offline  
Reply With Quote
The Following User Says Thank You to dumbterminal For This Useful Post:
alostpacket (January 4th, 2012)
Reply

Bookmarks


Go Back   Android Forums > Android Development > Application Development User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 12:01 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo