請爲此錯誤提供一個可能的解決方案。CursorIndexOutOfBoundsException - SQLITE錯誤
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;
}
發佈您的代碼:行沒有227 –