Stack Overflow已多次询问有关此特定错误消息的类似问题。我找不到符合我情况的那个。所以就这样了。
当我打开严格模式时,我在内容提供商的query
方法中收到此错误。我的内容提供商的查询方法在下面调用queryDirections(...)
。我只是调用db.query
方法并返回光标。我该如何关闭这个光标?编译器在下面的行返回语句中抱怨光标未关闭。详细的错误消息如下。
感谢。
内容提供商
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();
}
的ErrorMessage
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)
答案 0 :(得分:0)
Cursor
的 ContentProvider
返回必须由接收组件(Activity
,Service
等)关闭,而不是由ContentProvider
本身关闭。