Search code examples
androidcursorchat

Cursor is not close, How can I solve it?


my app runs fine in the ADT emulator but the problem is when I run my app in a real devie. It throws this error.

07-21 14:27:10.339: E/CursorLeakDetecter(17824): PossibleCursorLeak:content://com.mychat/account,QueryCounter:6
07-21 14:27:10.339: E/CursorLeakDetecter(17824): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
07-21 14:27:10.339: E/CursorLeakDetecter(17824):    at android.content.ContentResolver.query(ContentResolver.java:399)
07-21 14:27:10.339: E/CursorLeakDetecter(17824):    at android.content.ContentResolver.query(ContentResolver.java:316)
07-21 14:27:10.339: E/CursorLeakDetecter(17824):    at com.example.mychat2.net.AccountsAdapter.update(AccountsAdapter.java:64)
07-21 14:27:10.339: E/CursorLeakDetecter(17824):    at com.example.mychat2.net.activity.account.Accounts.update(Accounts.java:232)
07-21 14:27:10.339: E/CursorLeakDetecter(17824):    at com.example.mychat2.net.activity.account.Accounts.onResume(Accounts.java:91)
07-21 14:27:10.339: E/CursorLeakDetecter(17824):    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1190)
07-21 14:27:10.339: E/CursorLeakDetecter(17824):    at android.app.Activity.performResume(Activity.java:5200)
07-21 14:27:10.339: E/CursorLeakDetecter(17824):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2893)
07-21 14:27:10.339: E/CursorLeakDetecter(17824):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2935)
07-21 14:27:10.339: E/CursorLeakDetecter(17824):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1386)
07-21 14:27:10.339: E/CursorLeakDetecter(17824):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-21 14:27:10.339: E/CursorLeakDetecter(17824):    at android.os.Looper.loop(Looper.java:153)
07-21 14:27:10.339: E/CursorLeakDetecter(17824):    at android.app.ActivityThread.main(ActivityThread.java:5336)
07-21 14:27:10.339: E/CursorLeakDetecter(17824):    at java.lang.reflect.Method.invokeNative(Native Method)
07-21 14:27:10.339: E/CursorLeakDetecter(17824):    at java.lang.reflect.Method.invoke(Method.java:511)
07-21 14:27:10.339: E/CursorLeakDetecter(17824):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
07-21 14:27:10.339: E/CursorLeakDetecter(17824):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-21 14:27:10.339: E/CursorLeakDetecter(17824):    at dalvik.system.NativeStart.main(Native Method)

This is my code

public void update() {
        clear();

        Cursor cursor = activity.getContentResolver().query(ChatPU.ACCOUNT_URI, null, null, null, AccountDbHelper._ID);


        if (cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();

                int id = cursor.getInt(cursor.getColumnIndex(AccountDbHelper._ID));
                String jid = cursor.getString(cursor.getColumnIndex(AccountDbHelper.IDS));
                String enabled = cursor.getString(cursor.getColumnIndex(AccountDbHelper.ENABLED));

                Account account = new Account(id, jid);
                account.setEnabled(enabled.equals("1"));
                add(account);

        while (cursor.moveToNext());
            cursor.close();

        }

    }

I actually read that this error is because I need close my database or my cursor.. but I did it cursor.close(); This problem only comes when I test my app in a real device... so anyone can help me please?


Solution

  • Try calling close on the cursor after the while loop.

    while (cursor.moveToNext());
                //other code if needed here
    
            } 
    cursor.close();
    

    According to documentation, cursor.Close() closes the cursor, releasing all of its resources and making it completely invalid. So, you should close only once.

    Also, it is recommended you close the cursor in onDestroy

    Android SQlite Leak Problem with CursorAdapter