[英]Unable to resume Activity error
My Activity contains this code to get all images on the SD card: 我的活动包含此代码以获取SD卡上的所有图像:
String[] projection = {MediaStore.Images.Media._ID,
MediaStore.Images.Media.DATA,
MediaStore.Images.ImageColumns.DATA};
Cursor cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, null, null,
MediaStore.Images.Media._ID);
int count = cursor.getCount();
int image_path_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
int i;
for(i = 0; i < count; i++) {
cursor.moveToPosition(i);
String p = cursor.getString(image_path_index);
fd.addToPhonePhoto(p);
}
cursor.close();
The occurred while the Activity was resuming: 活动恢复时发生的事件:
03-14 14:06:48.380: E/AndroidRuntime(20793): java.lang.RuntimeException: Unable to resume activity {}: java.lang.RuntimeException: Unable to resume activity {}: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
03-14 14:06:48.380:E / AndroidRuntime(20793):java.lang.RuntimeException:无法恢复活动{}:java.lang.RuntimeException:无法恢复活动{}:android.database.StaleDataException:Attempted to关闭后访问游标。
It only occurs on Android 4.0. 它只发生在Android 4.0上。 If on Android 2.x or 3.x, it works normally.
如果在Android 2.x或3.x上,它可以正常工作。 But if I change the system setting which selects the "don't keep Activities" option in "Developer options".
但是,如果我更改了在“开发者选项”中选择“不保留活动”选项的系统设置。 The error does not show.
错误未显示。
I want to modify my code to avoid this error without changing the system setting. 我想修改我的代码以避免此错误而不更改系统设置。 How should I do it?
我该怎么办?
I think this is because of the managedQuery
call + you closing the cursor. 我认为这是因为在关闭游标时使用了
managedQuery
调用。 From the docs of the managedQuery()
method: 从
managedQuery()
方法的文档:
Warning: Do not call close() on a cursor obtained using this method, because the activity will do that for you at the appropriate time.
警告:不要对使用此方法获得的游标调用close(),因为活动将在适当的时候为您执行此操作。 However, if you call stopManagingCursor(Cursor) on a cursor from a managed query, the system will not automatically close the cursor and, in that case, you must call close().
但是,如果从托管查询的光标上调用stopManagingCursor(Cursor),系统将不会自动关闭光标,在这种情况下,您必须调用close()。
Leave the cursor for the Android system to manage and don't call cursor.close();
将光标留给Android系统进行管理,不要调用
cursor.close();
. 。
Note:
The managedQuery
method is deprecated and it should be avoided , implement CursorLoaders
instead. Note:
不推荐使用managedQuery
方法,应该避免使用它 ,而是实现CursorLoaders
。 More info about CursorLoaders
can be found at developer.android.com
. 有关
CursorLoaders
更多信息,请访问developer.android.com
。
remove cursor.close();
remove
cursor.close();
in your code it work fine definitely 在你的代码中它肯定工作正常
Function, managedQuery() is deprecated. 函数,managedQuery()已被弃用。
Please use getContentResolver().query(). 请使用getContentResolver()。query()。
The parameters is the same. 参数是相同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.