gpt4 book ai didi

Android - Activity 销毁期间的 android.database.StaleDataException

转载 作者:太空狗 更新时间:2023-10-29 14:39:36 33 4
gpt4 key购买 nike

目前我的应用程序仅在两台设备上遇到此崩溃,三星 Galaxy (SM-G950F [api 26] 和 Galaxy J7 [api 23])。
我认为他们是三星这一事实非常重要,因为我使用的所有其他测试设备(物理、LG [api 24] 和 Genymotion 的模拟器)从未发生过。

这是完整的堆栈跟踪:

Fatal Exception: java.lang.RuntimeException: Unable to destroy activity {us.highlanders.app/rs.highlande.highlanders_app.activities_and_fragments.activities_create_post.CreatePostActivityMod}: android.database.StaleDataException: Attempted to access a cursor after it has been closed.
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4603)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4621)
at android.app.ActivityThread.-wrap5(Unknown Source)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1757)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

Caused by android.database.StaleDataException: Attempted to access a cursor after it has been closed.
at android.database.BulkCursorToCursorAdaptor.throwIfCursorIsClosed(BulkCursorToCursorAdaptor.java:63)
at android.database.BulkCursorToCursorAdaptor.getCount(BulkCursorToCursorAdaptor.java:69)
at android.database.CursorWrapper.getCount(CursorWrapper.java:60)
at <MY-APP-PACKAGE>.adapters.CustomGalleryAdapter.getItemCount(CustomGalleryAdapter.java:111)
at android.support.v7.widget.RecyclerView$LayoutManager.getColumnCountForAccessibility(RecyclerView.java:10131)
at android.support.v7.widget.RecyclerView$LayoutManager.onInitializeAccessibilityNodeInfo(RecyclerView.java:9992)
at android.support.v7.widget.RecyclerView$LayoutManager.onInitializeAccessibilityNodeInfo(RecyclerView.java:9951)
at android.support.v7.widget.RecyclerViewAccessibilityDelegate.onInitializeAccessibilityNodeInfo(RecyclerViewAccessibilityDelegate.java:61)
at android.support.v4.view.AccessibilityDelegateCompat$AccessibilityDelegateApi16Impl$1.onInitializeAccessibilityNodeInfo(AccessibilityDelegateCompat.java:126)
at android.view.View.onInitializeAccessibilityNodeInfo(View.java:7951)
at android.view.View.createAccessibilityNodeInfoInternal(View.java:7912)
at android.view.View$AccessibilityDelegate.createAccessibilityNodeInfo(View.java:27397)
at android.view.View.createAccessibilityNodeInfo(View.java:7895)
at android.view.accessibility.AccessibilityRecord.setSource(AccessibilityRecord.java:146)
at android.view.accessibility.AccessibilityRecord.setSource(AccessibilityRecord.java:119)
at android.view.View.onInitializeAccessibilityEventInternal(View.java:7849)
at android.view.View$AccessibilityDelegate.onInitializeAccessibilityEvent(View.java:27280)
at android.support.v4.view.AccessibilityDelegateCompat.onInitializeAccessibilityEvent(AccessibilityDelegateCompat.java:309)
at android.support.v7.widget.RecyclerViewAccessibilityDelegate.onInitializeAccessibilityEvent(RecyclerViewAccessibilityDelegate.java:67)
at android.support.v4.view.AccessibilityDelegateCompat$AccessibilityDelegateApi16Impl$1.onInitializeAccessibilityEvent(AccessibilityDelegateCompat.java:120)
at android.view.View.onInitializeAccessibilityEvent(View.java:7835)
at android.view.View.sendAccessibilityEventUncheckedInternal(View.java:7699)
at android.view.View$AccessibilityDelegate.sendAccessibilityEventUnchecked(View.java:27219)
at android.support.v4.view.AccessibilityDelegateCompat.sendAccessibilityEventUnchecked(AccessibilityDelegateCompat.java:248)
at android.support.v4.view.AccessibilityDelegateCompat$AccessibilityDelegateApi16Impl$1.sendAccessibilityEventUnchecked(AccessibilityDelegateCompat.java:148)
at android.view.View.sendAccessibilityEventUnchecked(View.java:7682)
at android.support.v7.widget.RecyclerView.sendAccessibilityEventUnchecked(RecyclerView.java:3421)
at android.view.View$SendViewStateChangedAccessibilityEvent.run(View.java:27434)
at android.view.View$SendViewStateChangedAccessibilityEvent.runOrPost(View.java:27467)
at android.view.View.notifyViewAccessibilityStateChangedIfNeeded(View.java:11870)
at android.view.View.onFocusChanged(View.java:7544)
at android.view.View.clearFocusInternal(View.java:7404)
at android.view.View.unFocus(View.java:7437)
at android.view.ViewGroup.unFocus(ViewGroup.java:1084)
at android.view.ViewGroup.unFocus(ViewGroup.java:1086)
at android.view.ViewGroup.removeViewInternal(ViewGroup.java:5416)
at android.view.ViewGroup.removeViewInternal(ViewGroup.java:5403)
at android.view.ViewGroup.removeView(ViewGroup.java:5334)
at android.support.v4.view.ViewPager.removeView(ViewPager.java:1501)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1529)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3244)
at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:3235)
at android.support.v4.app.FragmentController.dispatchDestroy(FragmentController.java:265)
at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:390)
at android.support.v7.app.AppCompatActivity.onDestroy(AppCompatActivity.java:209)
at <MY-APP-PACKAGE>.HLActivity.onDestroy(HLActivity.java:164)
at <MY-APP-PACKAGE>.CreatePostActivityMod.onDestroy(CreatePostActivityMod.java:95)
at android.app.Activity.performDestroy(Activity.java:7462)
at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1255)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4590)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4621)
at android.app.ActivityThread.-wrap5(Unknown Source)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1757)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

我这里基本上有一个 Activity,它由锚定在屏幕底部的 ViewPager 组成,其中第一个 fragment 是一个 RecyclerView 和水平 LayoutManager 想要模仿 Telegram/Messenger 图片库。一切都由 CustomGalleryAdapter 类(扩展 RecyclerView.Adapter)处理,它使用 Cursor 从设备中获取媒体文件,并崩溃当,出于可访问性原因,在 Activity 销毁期间,再次引用游标。此 Activity 是在等待结果时创建的,所以当我完成后,我像往常一样调用 finish() 来传递结果。

带有 RecyclerView 的 fragment 监听 Loader 回调:

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = {
MediaStore.Files.FileColumns._ID,
MediaStore.Files.FileColumns.DATE_ADDED,
MediaStore.Files.FileColumns.DATA,
MediaStore.Files.FileColumns.MEDIA_TYPE
};
String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE
+ " OR "
+ MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO;
String sortOrder = String.format("%s limit 50 ", MediaStore.Files.FileColumns.DATE_ADDED +" DESC");

if (getActivity() != null) {
return new CursorLoader(
getActivity(),
MediaStore.Files.getContentUri("external"),
projection,
selection,
null,
sortOrder
);
}
else return null;
}

@Override
public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor data) {
galleryAdapter.changeCursor(data);
}

@Override
public void onLoaderReset(@NonNull Loader<Cursor> loader) {
galleryAdapter.changeCursor(null);
}`

并查看框架源代码,当它已经关闭时它会回调我的光标:

public int getColumnCountForAccessibility(Recycler recycler, State state) {
if (mRecyclerView == null || mRecyclerView.mAdapter == null) {
return 1;
}
return canScrollHorizontally() ? mRecyclerView.mAdapter.getItemCount() : 1;
}

调用 getItemCount() 方法:

@Override
public int getItemCount() {
return (myCursor == null) ? 0 : myCursor.getCount();
}

我认为我可以在 fragment 的 onDestroyView() 方法中将 mAdapter 字段设置为 null:

@Override
public void onDestroyView() {
super.onDestroyView();

if (galleryRecView != null)
galleryRecView.setAdapter(null);
galleryAdapter = null;
}

解决问题。但是,当我尝试使用 Samsung 测试实验室 进行测试时,该应用程序一直崩溃。

但非常奇怪的是,我试图在我的设备上调试那些可访问性行甚至没有被断点捕获...<​​/p>

有人有什么建议吗?

最佳答案

尝试以下解决方法:

@Override
public int getItemCount() {
return (myCursor == null || myCursor.isClosed()) ? 0 : myCursor.getCount();
}

关于Android - Activity 销毁期间的 android.database.StaleDataException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50572951/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com