Cursor获取的时候有异常的处理 java.lang.IllegalStateException: trying to requery an already closed 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
uri The URI of the content provider to query.
projection List of columns to return.
selection SQL WHERE clause.
selectionArgs The arguments to selection, if any ?s are pesent
sortOrder SQL ORDER BY clause.
Returns
  • The Cursor that was returned by query().
See Also
  • query(android.net.Uri, String[], String, String[], String)
  • startManagingCursor(Cursor)
上面介绍已经说明是由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
uri The URI, using the content:// scheme, for the content to retrieve.
projection A list of which columns to return. Passing null will return all columns, which is inefficient.
selection A 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.
selectionArgs You 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.
sortOrder How 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
  • Cursor

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


你可能感兴趣的:(String,null,query,performance,caching,returning)