0

我不明白爲什麼我得到這個錯誤:AsyncQueryHandler - IllegalStateException異常:嘗試重新打開已關閉的對象 - Android電子

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

我的錯誤,我意識到這是happenin g,因爲我已經覆蓋了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 
    } 
} 
相關問題