user1880779
user1880779

Reputation: 2078

Android CursorWindowAllocationException

I'm getting this error :

E/AndroidRuntime(8223): Caused by: android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=940 (# cursors opened by this proc=940)

I know its probably because I'm using the Cursor in a wrong way or not closing it at the right time. I think it could be because I'm filling the same cursor without closing/emptying it?

public static void NextAlarmTxt(){
        int dan = c.get(Calendar.DAY_OF_WEEK);
        long trenutnovrijeme = c.getTimeInMillis();
        long bazavrijeme;
        long pamti = 0;
        String danString = dani(dan);       
        Cursor CursorDan = DatabaseManager.getAllDataDay(danString);


        CursorDan.moveToFirst();
        if (!CursorDan.isAfterLast())
        {
            do
            {
                bazavrijeme = CursorDan.getInt(2);

                if (trenutnovrijeme<bazavrijeme)
                {
                    if (pamti==0)
                    {
                        pamti = bazavrijeme;
                    }

                    if (pamti>0)
                    {
                    if (pamti > bazavrijeme)
                    {
                        pamti = bazavrijeme;
                    }
                    }                   
                }
                if (trenutnovrijeme > bazavrijeme)
                {
                    dan = dan+1;
                    dani(dan);
                    CursorDan = DatabaseManager.getAllDataDay(danString);
                }
            }
            while (CursorDan.moveToNext());         
        }               
        CursorDan.close();
        text1.setText(new StringBuilder("Sljedeći : " ).append(pamti).toString());
    }       
    public static String dani(int dan){

    String danString = null;

        if (dan==1)
        {
        danString = "Nedjelja";
        }
        else if (dan==2)
        {
            danString = "Ponedjeljak";
        }
        else if (dan==3)
        {
            danString = "Utorak";
        }
        else if (dan==4)
        {
            danString = "Srijeda";
        }
        else if (dan==5)
        {
            danString = "Četvrtak";
        }
        else if (dan==6)
        {
            danString = "Petak";
        }
        else if (dan==7)
        {
            danString = "Subota";
        }
        return danString;
    }

Upvotes: 1

Views: 3586

Answers (1)

Gaurav Arora
Gaurav Arora

Reputation: 1815

Cursor Opened :

Cursor CursorDan = DatabaseManager.getAllDataDay(danString);

After that you iterate through the cursor,

do {
...
if (trenutnovrijeme > bazavrijeme)
                {
                    dan = dan+1;
                    dani(dan);
                    CursorDan = DatabaseManager.getAllDataDay(danString);
                }
....
}
while (CursorDan.moveToNext());  

Now, with in the iteration loop, you overwrite the existing cursor with a new one, which leaves the existing one open. I am not sure what you are trying to achieve, but you should not be nesting cursor iteration this way and even if you do that, you should do it in a correct sequece.

  1. Close existing cursor
  2. Retrieve new cursor
  3. Move new cursor to the first item by cursor.moveToFirst()

Upvotes: 4

Related Questions