Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-如何修复java.lang.IndexOutOfBoundsException错误:索引:0,大小:0_Java_Android_Sqlite_Arraylist - Fatal编程技术网

Android-如何修复java.lang.IndexOutOfBoundsException错误:索引:0,大小:0

Android-如何修复java.lang.IndexOutOfBoundsException错误:索引:0,大小:0,java,android,sqlite,arraylist,Java,Android,Sqlite,Arraylist,我需要有关IndexOutOfBoundsException错误的以下情况的帮助。我有两个类,一个是数据库类,它扩展了SQLiteOpenHelper,另一个将从数据库类查询的值与微调器上的某些选择进行比较 因此,在数据库类中,我有两个方法来执行数据库查询:getInventory()和getStock() getInventory()方法首先查询数据库,然后将光标值添加到arraylistcheckInventory。然后getStock()设置arraylist的返回方法 List<S

我需要有关
IndexOutOfBoundsException
错误的以下情况的帮助。我有两个类,一个是数据库类,它扩展了
SQLiteOpenHelper
,另一个将从数据库类查询的值与微调器上的某些选择进行比较

因此,在数据库类中,我有两个方法来执行数据库查询:
getInventory()
getStock()

getInventory()
方法首先查询数据库,然后将光标值添加到arraylist
checkInventory
。然后
getStock()
设置arraylist的返回方法

List<String> checkInventory = new ArrayList<>();

public void getInventory(int primaryKey) {

    // Select All Query
    String selectQuery = "Select * FROM " + TABLE_PRODUCT + " WHERE " + PRODUCER_FK + " =  \"" + primaryKey + "\"";
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.rawQuery(selectQuery, null);
    cursor.moveToFirst();

    while (cursor.isAfterLast() == false) {
        String productName = cursor.getString(1);
        String productQuantity = String.valueOf(cursor.getInt(3));
        String productPrice = String.valueOf(cursor.getInt(2));

        checkInventory.add(productName);
        checkInventory.add(productQuantity);
        checkInventory.add(productPrice);

        cursor.moveToNext();
    }
}
    public String getStock(int pos) {
        return checkInventory.get(pos);
    }
当然,还有另一个类负责设置xml上的文本等等。但是,当我运行应用程序时,我得到一个异常:
java.lang.IndexOutOfBoundsException:Index:0,Size:0。
错误指向数据库类

public String getStock(int pos) {
        return checkInventory.get(pos);
    }
在另一节课上

String name = db.getStock(0);
String quantity = db.getStock(1);
String price = db.getStock(2);

我是android开发的新手,很明显我做错了什么,所以有更好的方法吗?任何帮助都将不胜感激

您忘记调用getInventory方法,所以checkInventory列表是空的。

您忘记调用getInventory方法,所以checkInventory列表是空的。

为什么需要
cursor.isAfterLast()==false
?嘿,John,cursor.isAfterLast()==false以防止infinte循环。然后moveToNext将迭代游标中的所有行,但我可能错了,为什么需要
游标。isAfterLast()==false
?嘿,John,游标。isAfterLast()==false以防止内部循环。然后moveToNext将在光标中的所有行上迭代,但我可能会出错,可能类似于ArrayList checkInventory=db.getInventory(primaryKey)??这就足够了吗?getInventory方法没有任何返回值。您可能应该将其命名为“db.getInventory(primaryKey);”有趣的是,我不再收到错误,但我的getButton不再工作,它可以单击但没有响应,我是否又错过了什么?所以不要害怕,在onclick处理程序中放置一个断点,并在debuggin模式下启动应用程序。您应该学习如何分步调试应用程序。这真是一项非常方便的技能。哈哈@Josef,这听起来像希腊语,但让我先试试,我真的很想学习ArrayList checkInventory=db.getInventory(primaryKey)??这就足够了吗?getInventory方法没有任何返回值。您可能应该将其命名为“db.getInventory(primaryKey);”有趣的是,我不再收到错误,但我的getButton不再工作,它可以单击但没有响应,我是否又错过了什么?所以不要害怕,在onclick处理程序中放置一个断点,并在debuggin模式下启动应用程序。您应该学习如何分步调试应用程序。这真是一种很好的技巧。哈哈,约瑟夫,我听起来像希腊语,但让我先试试,我真的很想学
String name = db.getStock(0);
String quantity = db.getStock(1);
String price = db.getStock(2);