请为我提供此错误的可能解决方案。
Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 2
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at com.freedomkitchen.sonali.freedomkitchenAndroidApp.DB_Access.GetRecipes(DB_Access.java:241)
at com.freedomkitchen.sonali.freedomkitchenAndroidApp.User_Ingredients_Input.ViewRecipes(User_Ingredients_Input.java:227)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
这是代码。
public ArrayList<String> GetRecipes(String meal_cat_selected) {
SQLiteDatabase db = this.getReadableDatabase();
// Cursor cursor_recipes = db.rawQuery("SELECT " + RECIPE_NAME + " FROM " + RECIPES_TABLE_NAME + " where " + MEAL_CATEGORY+"=?", new String[]{meal_cat_selected});
Cursor cursor_recipes = db.rawQuery("SELECT " + RECIPE_NAME + " FROM " + RECIPES_TABLE_NAME + " where " + MEAL_CATEGORY+"= '"+meal_cat_selected+"'", null);
ArrayList<String> array_list_recipes = new ArrayList<String>();
while (cursor_recipes.isAfterLast() == false) {
array_list_recipes.add(cursor_recipes.getString(cursor_recipes.getColumnIndex(RECIPE_NAME)));
cursor_recipes.moveToNext();
Log.i("reci_based_on_meal_cat", cursor_recipes.getString(cursor_recipes.getColumnIndex(RECIPE_NAME)));
}
db.close();
return array_list_recipes;
}
答案 0 :(得分:1)
您必须将光标移动到下一条记录一行之前。
cursor_recipes.moveToNext();
array_list_recipes.add(cursor_recipes.getString(cursor_recipes.getColumnIndex(RECIPE_NAME)));
实际上,当它仍位于 -1 位置时,您正试图使用它。
答案 1 :(得分:0)
向上移动字符串Log.i("reci_based_on_meal_cat", cursor_recipes.getString(cursor_recipes...
或向下移动cursor_recipes.moveToNext();