0

私はこのエラーを取得していますなぜ私は理解していない:AsyncQueryHandler - IllegalStateExceptionが:再オープン既に閉じたオブジェクトへの試み - アンドロイド

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

私はAsyncQueryHandlerを使用して、私の製品テーブルを照会しようとしています。スタックトレースcom.sdvd.www.sstore.CartActivity$1.onQueryComplete(CartActivity.java:134)

は、このライン上を向いているが、私はそれを修正する方法を確認していない。ここで

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

は、スタックトレースです:ここでは

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

... 

} 

答えて

0

私の間違い私はこれが起こっていることに気づいたこれは、onQueryComplete()メソッドをオーバーライドし、そのメソッド内でcursor.close()を呼び出していたためです。私はcursor.close()を削除し、エラーはなくなりました。ここで

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 
    } 
} 
関連する問題