如何修复Android中的光标初始化误差



我的Inv.db具有以下内容

从库存中选择 *;

id|name|supplier|quanity|price
1|Product 1|Wallmart|10|50
2|Product 1|Wallmart|10|50

但是我得到了错误

Failed to read row 0, column -1 from a CursorWindow which has 2 rows, 3 columns.
Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

但是有四列不包括ID。

// Find the columns of pet attributes that we're interested in
        int nameColumnIndex = cursor.getColumnIndex(InventoryContract.InventoryEntry.COLUMN_PRO_NAME);
        int priceColumnIndex = cursor.getColumnIndex(InventoryContract.InventoryEntry.COLUMN_PRO_PRICE);
        int quanColumnIndex = cursor.getColumnIndex(InventoryContract.InventoryEntry.COLUMN_PRO_QUAN);
        int supColumnIndex = cursor.getColumnIndex(InventoryContract.InventoryEntry.COLUMN_PRO_SUP);

        // Read the pet attributes from the Cursor for the current pet
        String petName = cursor.getString(nameColumnIndex);
        String price = cursor.getString(priceColumnIndex);
        String quantity = cursor.getString(quanColumnIndex); // produces error

我不确定这里有什么问题。如果我删除了代码的最后一行

,我的代码将运行

您的光标已正确初始化,但找不到InventoryContract.InventoryEntry.COLUMN_PRO_QUAN

处的字符串列索引

确保存储在那里的字符串匹配列名是什么。现在可能是不正确的

相关内容

最新更新