【发布时间】:2016-12-14 15:52:59
【问题描述】:
我收到一些用户的 ACRA 异常报告,指出向我的 appwidget (RemoteViewService) 提供数据的游标已停用/关闭。它从来没有亲身发生在我身上,但在有点问题的地方发生的就足够了。
这是我的 RemoteViewService 的代码:
public static class ListItemService extends RemoteViewsService {
public RemoteViewsFactory onGetViewFactory(final Intent intent) {
return new RemoteViewsFactory() {
private MyCursor cursor;
public void onCreate() {
// Nothing
}
public synchronized void onDestroy() {
if (this.cursor != null)
this.cursor.close();
}
public synchronized RemoteViews getViewAt(int position) {
// Here I read from the cursor and it crashes with
// the stack trace below
}
public int getCount() {
return ((this.cursor != null) ? this.cursor.getCount() : 0);
}
public int getViewTypeCount() {
return 1;
}
public boolean hasStableIds() {
return true;
}
public long getItemId(int position) {
return position;
}
public RemoteViews getLoadingView() {
return null;
}
public synchronized void onDataSetChanged()
{
if (this.cursor != null)
this.cursor.close();
this.cursor = getApplicationCntext().getContentResolver().query(myUri, null, null, null, null);
}
};
堆栈跟踪因平台版本而异。例如,我在 4.0.3 上得到以下内容:
android.database.StaleDataException: Attempting to access a closed CursorWindow.Most probable cause: cursor is deactivated prior to calling this method.
在 2.3 上,我得到一个:
java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery
除了onDestroy() 和onDataSetChanged() 之外,我一生都无法弄清楚是谁或什么东西关闭了我的光标。一些用户报告说,当崩溃发生时,他们并没有积极地使用应用程序。
我怀疑对 ContentProvider 的多次调用可能会返回相同的光标,当我显示使用相同查询的 UI 时,它们会互相踩踏。但情况似乎并非如此,因为光标对象不同。
有什么想法吗?
【问题讨论】:
标签: android android-sqlite android-cursor android-appwidget