cyb*_*onk 18 android cursor illegalstateexception android-loadermanager
环境(Linux/Eclipse Dev for Xoom Tablet运行HoneyComb 3.0.1)
在我的应用程序中,我正在使用相机(startIntentForResult())来拍照.拍摄照片后,我得到了onActivityResult()回调,并且能够使用通过"拍照"意图传递的Uri加载位图.此时我的活动已恢复,尝试将图像重新加载到图库时出错:
FATAL EXCEPTION: main
ERROR/AndroidRuntime(4148): java.lang.RuntimeException: Unable to resume activity {...}:
java.lang.IllegalStateException: trying to requery an already closed cursor
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2243)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1019)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:126)
at android.app.ActivityThread.main(ActivityThread.java:3997)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: trying to requery an already closed cursor
at android.app.Activity.performRestart(Activity.java:4337)
at android.app.Activity.performResume(Activity.java:4360)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2205)
... 10 more
Run Code Online (Sandbox Code Playgroud)
我正在使用的唯一光标逻辑是在拍摄图像后,我使用以下逻辑将Uri转换为文件
String [] projection = {
MediaStore.Images.Media._ID,
MediaStore.Images.ImageColumns.ORIENTATION,
MediaStore.Images.Media.DATA
};
Cursor cursor = activity.managedQuery(
uri,
projection, // Which columns to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int fileColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
if (cursor.moveToFirst()) {
return new File(cursor.getString(fileColumnIndex));
}
return null;
Run Code Online (Sandbox Code Playgroud)
我有什么想法我做错了吗?
cyb*_*onk 23
看起来在Honeycomb API中不推荐使用managedQuery()调用.
doc for ManageQuery()读取:
This method is deprecated.
Use CursorLoader instead.
Wrapper around query(android.net.Uri, String[], String, String[], String)
that the resulting Cursor to call startManagingCursor(Cursor) so that the
activity will manage its lifecycle for you. **If you are targeting HONEYCOMB
or later, consider instead using LoaderManager instead, available via
getLoaderManager()**.
Run Code Online (Sandbox Code Playgroud)
另外我注意到我在查询后调用了cursor.close(),我猜这是禁止的.找到这个真正有用的链接.经过一番阅读后,我想出了这个似乎有效的改变.
// causes problem with the cursor in Honeycomb
Cursor cursor = activity.managedQuery(
uri,
projection, // Which columns to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
// -------------------------------------------------------------------
// works in Honeycomb
String selection = null;
String[] selectionArgs = null;
String sortOrder = null;
CursorLoader cursorLoader = new CursorLoader(
activity,
uri,
projection,
selection,
selectionArgs,
sortOrder);
Cursor cursor = cursorLoader.loadInBackground();
Run Code Online (Sandbox Code Playgroud)
为了记录,这里是我如何在我的代码中修复它(在Android 1.6及更高版本上运行):我的问题是我无意中通过调用CursorAdapter.changeCursor()来关闭托管游标.在更改光标之前调用适配器光标上的Activity.stopManagingCursor()解决了问题:
// changeCursor() will close current one for us: we must stop managing it first.
Cursor currentCursor = ((SimpleCursorAdapter)getListAdapter()).getCursor(); // *** adding these lines
stopManagingCursor(currentCursor); // *** solved the problem
Cursor c = db.fetchItems(selectedDate);
startManagingCursor(c);
((SimpleCursorAdapter)getListAdapter()).changeCursor(c);
Run Code Online (Sandbox Code Playgroud)
小智 5
FIX:使用context.getContentResolver().query而不是activity.managedQuery.
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, PROJECTION, null, null, null);
} catch(Exception e) {
e.printStackTrace();
}
return cursor;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23597 次 |
| 最近记录: |