代码之家  ›  专栏  ›  技术社区  ›  Eilean Laborde

光标不近,如何解决?

  •  1
  • Eilean Laborde  · 技术社区  · 12 年前

    我的应用程序在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(); 只有当我在真实设备中测试应用程序时,才会出现此问题。。。有人能帮我吗?

    1 回复  |  直到 12 年前
        1
  •  0
  •   Community Mohan Dere    9 年前

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

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

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

    此外,建议您在 onDestroy

    Android SQlite Leak Problem with CursorAdapter