如何修复“请确保在光标上明确调用close()”?

时间:2011-03-09 03:57:06

标签: android database cursor

我正在构建一个Android应用程序,其活动包含以下代码:

public class SavedSalesActivity extends Activity {

    protected ListView mSalesList;
    protected Cursor mSalesCursor;
    protected SalesCursorAdapter mSalesAdapter;
    protected SalesDbAdapter mDb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sales_list);

        mDb = new SalesDbAdapter(this);
        mDb.open();
        mSalesCursor = mDb.getSaved();
        startManagingCursor(mSalesCursor);

        mSalesAdapter = new SalesCursorAdapter(this, R.layout.sales_list_item, mSalesCursor,
                new String[0], new int[0]);

        mSalesList = (ListView)findViewById(R.id.sales_listview);
        mSalesList.setAdapter(mSalesAdapter);
        mSalesList.setEmptyView(findViewById(android.R.id.empty));
        mSalesList.setOnItemClickListener(new OnItemClickListener() {...});
    }

    @Override
    protected void onStart() {
        Log.v(TAG, "Starting SavedSalesActivity");

        mDb.open();

        mSalesCursor = mDb.getSaved();
        startManagingCursor(mSalesCursor);

        super.onStart();
    }

    @Override
    protected void onStop() {
        Log.v(TAG, "Stopping SavedSalesActivity");

        mSalesCursor.close();

        mDb.close();

        super.onStop();
    }
}

根据我的理解,这应该足以正确关闭我的所有资源,但是如果我打开此活动并使用Android设备退出按钮退出,我会在短暂(可变)时间段后在Logcat中得到此信息。

03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169): Releasing statement in a finalizer. Please ensure that you explicitly call close() on your cursor: SELECT * FROM sales ORDER BY featured DESC
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:62)
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1229)
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184)
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1264)
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169):     at com.maps.app.classifiedconcepts.sale.SalesDbAdapter.getSaved(SalesDbAdapter.java:149)
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169):     at com.maps.app.classifiedconcepts.sale.SavedSalesActivity.onCreate(SavedSalesActivity.java:113)
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169):     at android.os.Looper.loop(Looper.java:123)
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169):     at java.lang.reflect.Method.invokeNative(Native Method)
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169):     at java.lang.reflect.Method.invoke(Method.java:521)
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-08 19:33:58.492: WARN/SQLiteCompiledSql(5169):     at dalvik.system.NativeStart.main(Native Method)
03-08 19:33:58.503: ERROR/Database(5169): close() was never explicitly called on database '/data/data/com.maps.app.classifiedconcepts/databases/classified_concepts' 
03-08 19:33:58.503: ERROR/Database(5169): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
03-08 19:33:58.503: ERROR/Database(5169):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1810)
03-08 19:33:58.503: ERROR/Database(5169):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
03-08 19:33:58.503: ERROR/Database(5169):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851)
03-08 19:33:58.503: ERROR/Database(5169):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:844)
03-08 19:33:58.503: ERROR/Database(5169):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:540)
03-08 19:33:58.503: ERROR/Database(5169):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
03-08 19:33:58.503: ERROR/Database(5169):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:112)
03-08 19:33:58.503: ERROR/Database(5169):     at com.maps.app.classifiedconcepts.sale.SalesDbAdapter.open(SalesDbAdapter.java:163)
03-08 19:33:58.503: ERROR/Database(5169):     at com.maps.app.classifiedconcepts.sale.SavedSalesActivity.onCreate(SavedSalesActivity.java:112)
03-08 19:33:58.503: ERROR/Database(5169):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-08 19:33:58.503: ERROR/Database(5169):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-08 19:33:58.503: ERROR/Database(5169):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-08 19:33:58.503: ERROR/Database(5169):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-08 19:33:58.503: ERROR/Database(5169):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-08 19:33:58.503: ERROR/Database(5169):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 19:33:58.503: ERROR/Database(5169):     at android.os.Looper.loop(Looper.java:123)
03-08 19:33:58.503: ERROR/Database(5169):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-08 19:33:58.503: ERROR/Database(5169):     at java.lang.reflect.Method.invokeNative(Native Method)
03-08 19:33:58.503: ERROR/Database(5169):     at java.lang.reflect.Method.invoke(Method.java:521)
03-08 19:33:58.503: ERROR/Database(5169):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-08 19:33:58.503: ERROR/Database(5169):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-08 19:33:58.503: ERROR/Database(5169):     at dalvik.system.NativeStart.main(Native Method)

mSalesCursor = mDb.getSaved(); 行在我的SalesDbAdapter中调用以下代码:

public Cursor getSaved() {
return mDb.query(TABLE_NAME, null, null, null, null, null, KEY_FEATURED + " DESC"); 
}

我做错了什么?为什么我会收到这些例外?我特别困惑,因为我认为在初始化mSalesCursor之后立即调用startManagingCursor(mSalesCursor)可以避免在任何Activity Lifecycle方法中关闭Cursor的需要。

3 个答案:

答案 0 :(得分:2)

对于其他人来看这个答案,我总是用try / catch / finally语句包装我的Cursor用法。无论发生什么情况,这都可以确保光标关闭。

我经常使用它,甚至我做了一个Eclipse模板。

示例:

void foo() {
    Cursor c = null;
    try {
        c = db.get_somthing();
        do_something©;
    }       

    catch (Exception e) {
        print_err_msg(e);
    }

    finally {
        if (c != null) {
            c.close();
            c = null;
        }
    }
}

答案 1 :(得分:0)

对于这些类型的事情,您应该覆盖onResume()onPause()而不是onStart()onStop()。查看Activity Lifecycle文档。

答案 2 :(得分:0)

考虑致电startManagingCursor()