0

Stack Overflow 中已多次询问有关此特定错误消息的类似问题。我找不到与我的情况相匹配的。就这样吧。

query当我打开严格模式时,内容提供者的方法中出现此错误。我的内容提供者的查询方法调用queryDirections(...)如下。我只是调用该db.query方法并返回光标。如何关闭此光标?编译器在该游标未关闭下面的行 return 语句中抱怨。详细的错误信息如下。谢谢。

内容提供商

    private Cursor queryDirections(Uri uri, String[] columns, 
        String selection, String[] selectionArguments,
        String sortOrder) {
            SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
            return db.query(C.Db.TABLE_DIRECTIONS, columns, 
                     selection, selectionArguments, null, null, sortOrder);
    }

从活动中调用

continued...
case R.id.spinnerStops:
    AsyncQueryHandler handler = new AsyncQueryHandler(getContentResolver()) {
        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor stopCursor) {
            stopCursor.moveToFirst();
            int columnIndex = stopCursor.getColumnIndex(Stop.KEY_TAG);
            if (stopCursor.getCount() > 0) {
                mStopTag = stopCursor.getString(columnIndex);
                stopCursor.close();
            }
        };
    };
    handler.startQuery(1, null, C.ContentUri.STOP, 
                           new String[] { 
                               C.Db.TABLE_STOPS + "." + Stop.KEY_TITLE,
                               C.Db.TABLE_STOPS + "." + Stop.KEY_TAG }, 
                       "Stops._id=" + id, null, null);
    break;

我还为我的两个微调器适配器覆盖了 onStop

@Override
protected void onStop() {
    MyLogger.log("closing cursor");
    try {
        if (mStopsAdapter != null) {

            mStopsAdapter.getCursor().close();
            mStopsAdapter = null;
        }
        if (mDirectionAdapter != null) {
            mDirectionAdapter.getCursor().close();
            mDirectionAdapter = null;
        }
    } catch (Exception e) {
    }
    super.onStop();

}

错误信息

05-31 23:30:23.295: E/StrictMode(28918): Finalizing a Cursor that has not been deactivated or closed. database = /data/data/com.thuptencho.torontotransitbus/databases/appdb.db, table = directions, query = SELECT tag FROM directions WHERE _id=-999
05-31 23:30:23.295: E/StrictMode(28918): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
05-31 23:30:23.295: E/StrictMode(28918):    at android.database.sqlite.SQLiteCursor.<init>(SQLiteCursor.java:98)
05-31 23:30:23.295: E/StrictMode(28918):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:50)
05-31 23:30:23.295: E/StrictMode(28918):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
05-31 23:30:23.295: E/StrictMode(28918):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
05-31 23:30:23.295: E/StrictMode(28918):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
05-31 23:30:23.295: E/StrictMode(28918):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
05-31 23:30:23.295: E/StrictMode(28918):    at com.thuptencho.torontotransitbus.provider.TheContentProvider.queryDirections(TheContentProvider.java:169)
05-31 23:30:23.295: E/StrictMode(28918):    at com.thuptencho.torontotransitbus.provider.TheContentProvider.query(TheContentProvider.java:109)
05-31 23:30:23.295: E/StrictMode(28918):    at android.content.ContentProvider.query(ContentProvider.java:652)
05-31 23:30:23.295: E/StrictMode(28918):    at android.content.ContentProvider$Transport.query(ContentProvider.java:189)
05-31 23:30:23.295: E/StrictMode(28918):    at android.content.ContentResolver.query(ContentResolver.java:372)
05-31 23:30:23.295: E/StrictMode(28918):    at android.content.ContentResolver.query(ContentResolver.java:315)
05-31 23:30:23.295: E/StrictMode(28918):    at android.content.AsyncQueryHandler$WorkerHandler.handleMessage(AsyncQueryHandler.java:79)
05-31 23:30:23.295: E/StrictMode(28918):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 23:30:23.295: E/StrictMode(28918):    at android.os.Looper.loop(Looper.java:137)
05-31 23:30:23.295: E/StrictMode(28918):    at android.os.HandlerThread.run(HandlerThread.java:60)
4

1 回答 1

0

Cursor从 a 返回的 sContentProvider必须由接收组件(ActivityService等)而不是ContentProvider自身关闭。

于 2013-06-01T04:18:20.020 回答