Cursor获取的时候有异常的处理 java.lang.IllegalStateException: trying to requery an already closed cursor

本文介绍了解决Android应用中由于Cursor管理不当导致的异常问题。详细分析了使用managedQuery方法时可能出现的问题,并给出了替代方案,避免因Cursor被关闭而引发的错误。

在获取memo的时候有一个根据Uri

获取Cursor的方法是用的Activity的方法:mCursor = managedQuery(mUri, PROJECTION, null, null, null);


public final Cursor managedQuery (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
Since: API Level 1

This method is deprecated.
Use CursorLoader instead.

Wrapper around query(android.net.Uri, String[], String, String[], String) that gives 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().

Parameters
uriThe URI of the content provider to query.
projectionList of columns to return.
selectionSQL WHERE clause.
selectionArgsThe arguments to selection, if any ?s are pesent
sortOrderSQL ORDER BY clause.
Returns
  • The Cursor that was returned by query().
上面介绍已经说明是由activity在通过query获取了Cursor之后用startManagingCursor来管理Cursor的生命周期的,那么每一次调用完毕之后Cursor也会相应的被关闭;由此从history menu tab进入的时候则可能因为Cursor被关闭了而导致异常:

E/AndroidRuntime( 1051): java.lang.RuntimeException: Unable to resume activity {com.lenovo.leos.memowidget/com.lenovo.leos.notepad.NoteEditor}: java.lang.IllegalStateException: trying to requery an already closed cursor
E/AndroidRuntime( 1051):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2227)
E/AndroidRuntime( 1051):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2255)
E/AndroidRuntime( 1051):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1028)
E/AndroidRuntime( 1051):     at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 1051):     at android.os.Looper.loop(Looper.java:132)
E/AndroidRuntime( 1051):     at android.app.ActivityThread.main(ActivityThread.java:4025)
E/AndroidRuntime( 1051):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1051):     at java.lang.reflect.Method.invoke(Method.java:491)
E/AndroidRuntime( 1051):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
E/AndroidRuntime( 1051):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
E/AndroidRuntime( 1051):     at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 1051): Caused by: java.lang.IllegalStateException: trying to requery an already closed cursor
E/AndroidRuntime( 1051):     at android.app.Activity.performRestart(Activity.java:4394)
E/AndroidRuntime( 1051):     at android.app.Activity.performResume(Activity.java:4420)
E/AndroidRuntime( 1051):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2217)
E/AndroidRuntime( 1051):     ... 10 more
W/ActivityManager(  124):   Force finishing activity com.lenovo.leos.memowidget/com.lenovo.leos.notepad.NoteEditor
W/ActivityManager(  124): Activity pause timeout for ActivityRecord{40472ac8 com.lenovo.leos.memowidget/com.lenovo.leos.notepad.NoteEditor}
E/Launcher( 1029): onResume


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

如果使用query则单纯的将Cursor返回来,就不会出现上述异常了。

public final Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
Since: API Level 1

Query the given URI, returning a Cursor over the result set.

For best performance, the caller should follow these guidelines:

  • Provide an explicit projection, to prevent reading data from storage that aren't going to be used.
  • Use question mark parameter markers such as 'phone=?' instead of explicit values in the selection parameter, so that queries that differ only by those values will be recognized as the same for caching purposes.
Parameters
uriThe URI, using the content:// scheme, for the content to retrieve.
projectionA list of which columns to return. Passing null will return all columns, which is inefficient.
selectionA filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given URI.
selectionArgsYou may include ?s in selection, which will be replaced by the values from selectionArgs, in the order that they appear in the selection. The values will be bound as Strings.
sortOrderHow to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.
Returns
  • A Cursor object, which is positioned before the first entry, or null
See Also

以上就是如何解决pad记事本的从历史进入memo时出现上述异常的bug了。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值