当前位置: 动力学知识库 > 问答 > 编程问答 >

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

问题描述:

I don't understand why I'm getting this error:

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

I'm trying to query my product table using AsyncQueryHandler.

In the stacktrace com.sdvd.www.sstore.CartActivity$1.onQueryComplete(CartActivity.java:134) is pointing on this line but I am not sure how fix it:

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

Here is the 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)

Here is my method where the error is occurring code:

 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);

...

}

网友答案:

My mistake I realised this is happening because I have overriden the onQueryComplete() method and I was calling cursor.close() inside that method. I removed the cursor.close() and it the error is gone.

Here is the class that contains the onQueryComplete():

public class QueryProductQtyAsyncQueryHandler extends AsyncQueryHandler {

    public QueryProductQtyAsyncQueryHandler(ContentResolver cr) {
        super(cr);
    }

    @Override
    protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
       // cursor.close(); //removed
    }
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: