2016-05-03 48 views
-2

請爲此錯誤提供一個可能的解決方案。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; 
} 
+2

發佈您的代碼:行沒有227 –

回答

0

向上移動字符串Log.i("reci_based_on_meal_cat", cursor_recipes.getString(cursor_recipes...

或向下移動User_Ingredients_Input.java的cursor_recipes.moveToNext();

1

您必須將光標移動到下一個記錄一個前行

cursor_recipes.moveToNext(); 
array_list_recipes.add(cursor_recipes.getString(cursor_recipes.getColumnIndex(RECIPE_NAME))); 

其實,你要使用它,而它仍然位於位置-1