一部のユーザーから、appwidget にデータを提供するカーソル ( RemoteViewService
) が非アクティブ化/クローズされているという ACRA 例外レポートを受け取りました。それは私に直接起こることは決してありませんが、それが少し問題になる場合には十分に起こります.
私の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
私の人生では、 fromonDestroy()
とonDataSetChanged()
. 一部のユーザーは、クラッシュが発生したときにアプリを積極的に操作していなかったと報告しています。
ContentProvider への複数の呼び出しが同じカーソルを返す可能性があり、同じクエリを使用する UI を表示すると、それらは互いにステップします。しかし、カーソル オブジェクトが異なるため、そうではないようです。
何か案は?