我的主要活动(比如说A)提出了从ContactsContract通过
获取ContactID的意图startActivityForResult(new Intent(Intent.ACTION_PICK, contactsContract.Contacts.CONTENT_URI),PICK_CONTACT);
在返回(onActivityResult(int reqCode,int resultCode,Intent data))时,我正在使用' data'通过
查询联系人 if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
......
我的代码适用于Android 2.x.但是,此代码在ICS(Android 4.x)中崩溃,同时恢复主Activity(A)。 我不确定,是否是由于被弃用的“托管查询”而导致的。 有人可以提供一些解决方案,我卡住甚至无法找到哪一行导致错误,因为Log cat甚至没有显示错误行号。它简单地说 引起:android.database.StaleDataException:在游标关闭后尝试访问它。 提前致谢 Joshy
我在完成光标要求后尝试了上面的代码。问题仍然存在。我的Log cat说如下
08-13 00:26:31.427: W/dalvikvm(3176): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
08-13 00:26:31.447: E/AndroidRuntime(3176): FATAL EXCEPTION: main
08-13 00:26:31.447: E/AndroidRuntime(3176): java.lang.RuntimeException: Unable to resume activity {com.desquare.sp/com.desquare.sp.SmartPadActivity}: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2444)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2472)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1173)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.os.Looper.loop(Looper.java:137)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-13 00:26:31.447: E/AndroidRuntime(3176): at java.lang.reflect.Method.invokeNative(Native Method)
08-13 00:26:31.447: E/AndroidRuntime(3176): at java.lang.reflect.Method.invoke(Method.java:511)
08-13 00:26:31.447: E/AndroidRuntime(3176): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-13 00:26:31.447: E/AndroidRuntime(3176): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-13 00:26:31.447: E/AndroidRuntime(3176): at dalvik.system.NativeStart.main(Native Method)
08-13 00:26:31.447: E/AndroidRuntime(3176): Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.database.BulkCursorToCursorAdaptor.throwIfCursorIsClosed(BulkCursorToCursorAdaptor.java:75)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.database.BulkCursorToCursorAdaptor.requery(BulkCursorToCursorAdaptor.java:144)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.database.CursorWrapper.requery(CursorWrapper.java:186)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.app.Activity.performRestart(Activity.java:4505)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.app.Activity.performResume(Activity.java:4531)
08-13 00:26:31.447: E/AndroidRuntime(3176): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2434)
08-13 00:26:31.447: E/AndroidRuntime(3176): ... 10 more
答案 0 :(得分:6)
听起来这就是问题所在:
* android.database.staledataexception : Access closed cursor
您似乎正在使用managedQuery获取游标。
我在恢复应用程序方面遇到了问题 冰淇淋三明治没有正确关闭游标。修复是为了 在使用它们之后立即对所有游标执行此操作:
if (cursor != null && !cursor.isClosed()) {
myActivity.stopManagingCursor( cursor );
cursor.close();
}
答案 1 :(得分:0)
不推荐使用API startManagingCursor / stopManagingCursor,这是问题的根本原因。 例如,请对此进行评论并验证。