AsyncQueryHandler - IllegalStateException:尝试重新打开已经关闭的对象--Android

时间:2016-12-27 15:54:39

标签: android android-asynctask android-sqlite android-contentprovider

我不明白为什么我会收到此错误:

IllegalStateException: attempt to re-open an already-closed object: 

我尝试使用AsyncQueryHandler查询我的产品表。

在stacktrace com.sdvd.www.sstore.CartActivity$1.onQueryComplete(CartActivity.java:134)指向此行,但我不确定如何解决它:

if ((cursor != null) && cursor.moveToFirst())

这是stacktrace:

 IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT _id, productQuantity, productName FROM product WHERE productName= ?
      at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
      at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:58)
      at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:151)
      at android.database.sqlite.SQLiteCursor.onMove(SQLiteCursor.java:123)
      at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:236)
      at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:258)
      at android.database.CursorWrapper.moveToFirst(CursorWrapper.java:71)
      at com.sdvd.www.sstore.CartActivity$1.onQueryComplete(CartActivity.java:134)
      at android.content.AsyncQueryHandler.handleMessage(AsyncQueryHandler.java:344)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:148)
      at android.app.ActivityThread.main(ActivityThread.java:5417)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

这是我的方法,其中发生错误代码:

    public void updateItem(boolean active, int position) {

        String[] projection = {
                ProductContract.ProductEntry._ID,
                ProductContract.ProductEntry.COLUMN_PRODUCT_QUANTITY,
                ProductContract.ProductEntry.COLUMN_PRODUCT_NAME};


        TextView cartProductName = (TextView) findViewById(R.id.cart_product_name);



        QueryProductQtyAsyncQueryHandler addStockUsingBackgroundThread = new QueryProductQtyAsyncQueryHandler(this.getContentResolver()) {
            @Override
            protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
                super.onQueryComplete(token, cookie, cursor);

                Log.v("Inside onQueryComplete", cursor.toString() );
                //DatabaseUtils.dumpCursor(cursor);

                if ((cursor != null) && cursor.moveToFirst()) {
                    // get the quantity?
                    int productQtyColumnIndex = cursor.getColumnIndex(ProductContract.ProductEntry.COLUMN_PRODUCT_QUANTITY);
                    currentStockQuantity = cursor.getString(productQtyColumnIndex);
                    cartProductQtyTxtView.setText(currentStockQuantity);
                    //return;
                }
            }
        };
        addStockUsingBackgroundThread.startQuery(1
                , null
                , ProductContract.ProductEntry.CONTENT_URI
                , projection
                , ProductContract.ProductEntry.COLUMN_PRODUCT_NAME + "= ?"
                , new String[]{cartProductName.getText().toString()}
                , null);

...

}

1 个答案:

答案 0 :(得分:0)

我的错误我意识到这种情况正在发生,因为我已经覆盖了onQueryComplete()方法,并且我在该方法中调用了cursor.close()。我删除了cursor.close(),错误就消失了。

这是包含onQueryComplete()的类:

.headerimg > a {
    display: block;
    width: 100%; /*or whatever fits your need*/
    height: 100%; /*or whatever fits your need*/
    background: url('/images/small_image.jpg') center center cover no-repeat;
}
@media (min-width: 600px){
    background-image: url('/images/large_image.jpg');
}