2016-05-03 5 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

あなたは前に、次のレコード1行にカーソルを移動しなければなりません。

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

実は、あなたはそれはまだポジション-1に位置している間、それを使用しようとしています。