错误:Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been closed
android4.0以后会自动关闭游标,所以在使用后,高于4.0版本,手动关闭就会报上述错误
Uri uri = Uri.parse("content://com.zjsos.www.databasetest.provider/book");
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
String author = cursor.getString(cursor.getColumnIndex("author"));
int pages = cursor.getInt(cursor.getColumnIndex("pages"));
double price = cursor.getDouble(cursor.getColumnIndex("price"));
if (Integer.parseInt(Build.VERSION.SDK) < 14) {
//只有4.0以下才需要手动关闭
cursor.close();
}
}
}