光标不接近,如何解决?



我的应用程序在ADT模拟器中运行良好,但问题是当我在实际设备中运行应用程序时。它抛出这个错误。

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 ();只有当我在真实设备上测试应用时才会出现这个问题……有人能帮帮我吗?

尝试在while循环之后对游标调用close。

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

根据文档,cursor.Close()关闭游标,释放其所有资源,并使完全无效。因此,您应该只关闭一次。

同时,建议您关闭onDestroy

中的光标。

Android SQlite泄漏问题与CursorAdapter

最新更新