光标不靠近,我该如何解决?

时间:2014-07-21 21:15:36

标签: android cursor chat

我的应用程序在ADT模拟器中运行良好,但问题是我在真正的devie中运行我的应用程序。它抛出了这个错误。

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)

这是我的代码

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();

        }

    }

我实际上读到这个错误是因为我需要关闭我的数据库或我的光标..但我做到了    cursor.close(); 只有当我在真实设备中测试我的应用程序时才会出现此问题...所以任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

在while循环后尝试在光标上调用close。

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

        } 
cursor.close();

根据documentation,cursor.Close() 关闭光标,释放所有资源,使其完全无效。所以,你应该只关闭一次。

此外,建议您关闭onDestroy

中的光标

Android SQlite Leak Problem with CursorAdapter