错误无法从 CursorWindow 读取第 0 行,第 -1 列。在从游标访问数据之前确保正确初始化了游标

Error Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it

我遇到错误

Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

我已经尝试了所有方法,但似乎没有任何效果

在 运行 项目之后,我的应用程序停止在这个方法上:

getPriceDataByNameProductAndSupplier

代码:

/*_____________________________ My query _______________________*/
public double getPriceDataByNameProductAndSupplier(int product_id,int supplier_id)
{

Product product=null;
double  Price_for_every_one=0.0;
String query= "select max(price_for_every_one) from product_Multi_Value where _id ="+ product_id +" and _idSupplier="+ supplier_id ;
Cursor cursor = sqLiteDatabase.rawQuery(query,null/*ew String[] {String.valueOf(product_id),
        String.valueOf(supplier_id)}*/); /*("product_Multi_Value",
                                     new String[] {"MAX(price_for_every_one) AS MAX"},"_id=? and _idSupplier=?",
                                     new String[] {String.valueOf(product_id),String.valueOf(supplier_id)},
                                     null,null,null)*/;// cursor.moveToFirst();
if (cursor !=null)
{
    cursor.moveToFirst();
    do
    {
        Price_for_every_one = cursor.getDouble(cursor.getColumnIndexOrThrow("price_for_every_one") );
        product = new Product(Price_for_every_one);
    }
    while (cursor.moveToNext());
}
return Price_for_every_one;
}

Table 创作

/_______________________(我的Table)_______________________/ String Crete_Table_Product_For_Multi_Value = "创建 TABLE 如果不存在 product_Multi_Value( Product_Type 文本不为空,product_date 文本不为空,supplier_date 时间戳 默认 CURRENT_TIMESTAMP,exp_date TEXT NOT NULL, quantity_package REAL NOT NULL, number_quantity_ber_package 整数不为空,total_of_quantity 实数不为空, price_for_Allquantity 实数不为空,price_for_every_one 实数不为空, price_for_sales REAL,_id INTEGER NOT NULL,idSupplier INTEGER NOT NULL,FOREIGN KEY( _idSupplier) REFERENCES 供应商(_id), FOREIGN KEY(_id) REFERENCES 产品(_id));";

错误日志:

错误:

E/CursorWindow: Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 1 columns.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ans.accouting_s_p, PID: 1349
java.lang.IllegalStateException: Could not execute method for android:onClick

at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390) at android.view.View.performClick(View.java:5623) at android.view.View$PerformClick.run(View.java:22433) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6316) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) at android.view.View.performClick(View.java:5623)  at android.view.View$PerformClick.run(View.java:22433)  at android.os.Handler.handleCallback(Handler.java:751)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6316)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)  Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. at android.database.CursorWindow.nativeGetDouble(Native Method) at android.database.CursorWindow.getDouble(CursorWindow.java:543) at android.database.AbstractWindowedCursor.getDouble(AbstractWindowedCursor.java:87) at com.ans.accouting_s_p.databaseTable.DataSourceProduct.getpriceDataByNameProductAndSupplier(DataSourceProduct.java:322)

问题是您正在尝试访问游标中的列 price_for_every_one 但它不存在,因为当您使用 MAX、MIN、SUM 等聚合方法时 它更改了结果中列的名称,除非您使用 AS 关键字

在您的 SQL 查询中

String query = "select max(price_for_every_one) from product_Multi_Value where _id ="+ product_id +" and _idSupplier="+ supplier_id ;

结果中的列名将是

max(price_for_every_one)

不是

price_for_every_one

所以你不能使用

中的列名
Price_for_every_one = cursor.getDouble(cursor.getColumnIndexOrThrow("price_for_every_one") );

由于您只选择了一列,因此您可以通过索引访问该列

cursor.getDouble(0)

或者您可以像这样在查询中使用 AS 关键字

String query = "select max(price_for_every_one) As price_for_every_one from product_Multi_Value where _id ="+ product_id +" and _idSupplier="+ supplier_id ;```

希望对您有所帮助