尝试重新查询一个已经关闭的游标

尝试重新查询一个已经关闭的游标

本文介绍了Android的错误:java.lang.IllegalStateException:尝试重新查询一个已经关闭的游标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境(Linux的/ Eclipse的开发为Xoom平板电脑上运行的蜂窝3.0.1)

environment (Linux/Eclipse Dev for Xoom Tablet running HoneyComb 3.0.1)

在我的应用程序,我用相机(startIntentForResult())拍照。在拍摄完照片后,我得到了onActivityResult()回调是能够加载使用URI通过拍照的意图通过一个位图。在这一点上我的活动重新开始,我得到一个错误尝试将图像加载到画廊:

In my app I'm using the camera (startIntentForResult()) to take a picture. After the picture is taken I get the onActivityResult() callback and am able to load a Bitmap using a Uri passed via the "take picture" intent. At that point my activity is resumed and I get an error trying to reload the images into a gallery:

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

我使用的唯一的游标逻辑是拍摄后的图像使用以下逻辑我转换的URI到文件

The only cursor logic I'm using is that after the image is taken I convert the Uri to a file using the following logic

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;

任何想法,我做错了什么?

Any ideas what I'm doing wrong?

推荐答案

看起来像managedQuery()调用pcated的蜂窝API中去$ P $。

Looks like the managedQuery() call is deprecated in the Honeycomb API.

文档的managedQuery()读取:

Doc for managedQuery() reads:

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()**.

此外,我注意到,我在呼唤cursor.close(),我想查询后,一个没有没有。发现这个<一href="http://groups.google.com/group/android-developers/browse_thread/thread/0658133bec901d7e/d9fb4c7a8355c22d?show_docid=d9fb4c7a8355c22d"相对=nofollow> 以及真正有用的链接。一些阅读后,我想出了这种变化,似乎工作。

Also I noticed that I was calling cursor.close() after the query which I guess is a no-no. Found this really helpful link as well. After some reading I came up with this change that seems to work.

// 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();

这篇关于Android的错误:java.lang.IllegalStateException:尝试重新查询一个已经关闭的游标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 17:56