Java:带有SimpleCursorAdapter的ORMLite:StaleDataException

时间:2013-01-09 10:28:41

标签: java simplecursoradapter ormlite staledataexception

这是我第一次测试SimpleCursorAdapter。我想和ORMLite一起使用。

不幸的是,我总是得到StaleDataException :(

编辑: 不确定如果adapter.swapCursor(c)是问题。没有问题就走了过来。问题似乎在其他地方 / END EDIT

错误似乎发生在adapter.swapCursor(c)中。 c是关闭的,这似乎是问题所在,我该如何解决这个问题,为什么它会被关闭? 注意:仅当Einsatz1 sqllite表包含数据时才会出现错误。如果桌子是空的,一切都会完美运行。

这是我的活动:

public class UebersichtEinsaetze extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor>
{
...

  public DatabaseHelper helper = null;

  @Override
  protected void onDestroy()
  {
    super.onDestroy();
    ((SimpleCursorAdapter) einsatzListView.getAdapter()).getCursor().close();
    helper.close();
    if (helper != null)
    {
      OpenHelperManager.releaseHelper();
      helper = null;
    }

  }

  private DatabaseHelper getHelper()
  {
    if (helper == null)
    {
      helper = OpenHelperManager.getHelper(this, DatabaseHelper.class);
    }
    return helper;
  }

  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_ubersicht_einsaetze);
    einsatzListView = (ListView) findViewById(android.R.id.list);
    einsatzDatumTextView = (TextView) findViewById(R.id.tv_uebersicht_abdatum);
    fillData();
    getHelper();
  }

  private void fillData()
  {
    String[] from = new String[] { "_id" };
    int[] to = new int[] { R.id.view_list_einsatz_text_projnr };

    adapter = new SimpleCursorAdapter(this, R.layout.view_list_eintrag_einsatz, null, from, to, 0);
    getSupportLoaderManager().initLoader(0, null, this);
    einsatzListView.setAdapter(adapter);
  }

  @Override
  public Loader<Cursor> onCreateLoader(int id, Bundle args)
  {
    String[] projection = { "_id" };
    CursorLoader cursorLoader = new CursorLoader(this, test.CONTENT_URI, projection, null, null, null);
    return cursorLoader;
  }

  @Override
  public void onLoadFinished(Loader<Cursor> loader, Cursor c)
  {
    adapter.swapCursor(c);
  }

  @Override
  public void onLoaderReset(Loader<Cursor> loader)
  {
    adapter.swapCursor(null);
  }
}

这是我的Contentprovider:

 private DatabaseHelper database;
  private RuntimeExceptionDao<Einsatz1, String> dao;

  // Used for the UriMacher
  private static final int EINSAETZE = 10;
  private static final int EINSATZ_ID = 20;

  private static final String AUTHORITY = "de.myapp.android";

  private static String BASE_PATH = "einsaetze";
  public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);

  public static final String CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/einsaetze";
  public static final String CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/einsatz";

  private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  static
  {
    sURIMatcher.addURI(AUTHORITY, BASE_PATH, EINSAETZE);
    sURIMatcher.addURI(AUTHORITY, BASE_PATH + "/#", EINSATZ_ID);
  }

@Override
  public boolean onCreate()
  {
    database = new DatabaseHelper(getContext());
    this.dao = (RuntimeExceptionDao<Einsatz1, String>) database.getRTE(Einsatz1.class);
    return false;
  }

@Override
  public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
  {
    // ignore method parameters for now just to get it basically working

    QueryBuilder<Einsatz1, String> queryBuilder = this.dao.queryBuilder();

    Cursor cursor = null;
    CloseableIterator<Einsatz1> iterator = null;
    try
    {
      iterator = dao.iterator(queryBuilder.prepare());
      AndroidDatabaseResults results = (AndroidDatabaseResults) iterator.getRawResults();
      cursor = results.getRawCursor();
    }
    catch (SQLException e)
    {
      Log.e(getClass().getName(), "get cursor", e);
    }
    finally
    {
      iterator.closeQuietly();
    }

    // Make sure that potential listeners are getting notified
    cursor.setNotificationUri(getContext().getContentResolver(), uri);

    return cursor;
  }

logcat的:

01-09 10:01:29.591: E/AndroidRuntime(3767): FATAL EXCEPTION: main
01-09 10:01:29.591: E/AndroidRuntime(3767): android.database.StaleDataException: Attempting to access a closed CursorWindow.Most probable cause: cursor is deactivated prior to calling this method.
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:139)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.database.CursorWrapper.getString(CursorWrapper.java:114)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.support.v4.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:135)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:256)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.widget.AbsListView.obtainView(AbsListView.java:2267)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.widget.ListView.onMeasure(ListView.java:1156)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.view.View.measure(View.java:15172)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.view.View.measure(View.java:15172)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.view.View.measure(View.java:15172)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.view.View.measure(View.java:15172)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.view.View.measure(View.java:15172)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.view.Choreographer.doFrame(Choreographer.java:525)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.os.Handler.handleCallback(Handler.java:615)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.os.Looper.loop(Looper.java:137)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at android.app.ActivityThread.main(ActivityThread.java:4745)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at java.lang.reflect.Method.invokeNative(Native Method)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at java.lang.reflect.Method.invoke(Method.java:511)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-09 10:01:29.591: E/AndroidRuntime(3767):     at dalvik.system.NativeStart.main(Native Method)

小记(不重要,但如果有人有信息......): 当我在adapter.swapCursor(c)之前设置断点时,它在抛出错误之前不会停在那里。我必须设置一个断点来在该语句之前插入一个虚拟的“int bla = 123”。类似地,在公共Cursor query(...)方法中的任何位置设置断点会使所有内容执行而不会出错(这是怎么回事......),但同样不会在断点处停止。

2 个答案:

答案 0 :(得分:4)

<强> 解决

评论

iterator.closeQuietly();

即使我发现这里的代码来自:

Android Cursor with ORMLite to use in CursorAdapter

......你不应该依赖它。

我将不得不看看评论会产生什么潜在的负面影响(如果有人知道请告诉我),但现在一切似乎都在起作用。

答案 1 :(得分:1)

是的,它看起来是由于iterator.closeQuietly(),虽然注释的影响是你现在可能有一个泄漏,许多迭代器打开(应该真的关闭)。

相反,在你的onQuery()方法中,尝试使用以下内容,它将为你提供对Cursor的引用,当你的ContentProvider关闭时,它应该关闭。

Cursor cursor = getHelper().getReadableDatabase().query(tableName, projection, selection, selectionArgs, groupBy, having, sortOrder)