抛出异常:rowIndex>可用行:2> 1

时间:2016-11-07 16:59:18

标签: android realm

我实际上正在使用Realm进行应用程序,而且我遇到了一些麻烦。

我在我的活动中加载一个列表,其中包含存储在我的Realm DB中的一些信息,它在列表中的2个元素上运行得很好但是当我添加第三个并尝试加载活动时,这个异常就是抛出

11-07 16

:54:28.829 11829-11829/com.list.buffet.alvaro.buffetlist E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.list.buffet.alvaro.buffetlist/com.list.buffet.alvaro.buffetlist.SavedListActivity}: java.lang.ArrayIndexOutOfBoundsException: rowIndex > available rows: 2 > 1
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2351)
    at android.app.ActivityThread.handleLauchActivity(ActivityThread.java:2403)
    at android.app.ActivityThread.access$600(ActivityThread.java:165)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5391)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ArrayIndexOutOfBoundsException: rowIndex > available rows: 2 > 1
    at io.realm.internal.TableView.nativeGetSourceRowIndex(Native Method)
    at io.realm.internal.TableView.getSourceRowIndex(TableView.java:111)
    at io.realm.RealmResults.get(RealmResults.java:215)
    at com.list.buffet.alvaro.buffetlist.SavedListActivity.saveListInMap(SavedListActivity.java:166)
    at com.list.buffet.alvaro.buffetlist.SavedListActivity.addElementsToList(SavedListActivity.java:156)
    at com.list.buffet.alvaro.buffetlist.SavedListActivity.onCreate(SavedListActivity.java:43)
    at android.app.Activity.performCreate(Activity.java:5122)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1150)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403) 
    at android.app.ActivityThread.access$600(ActivityThread.java:165) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373) 
    at android.os.Handler.dispatchMessage(Handler.java:107) 
    at android.os.Looper.loop(Looper.java:194) 
    at android.app.ActivityThread.main(ActivityThread.java:5391) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:525) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 
    at dalvik.sys

抛出此异常的方法是:

private void saveListInMap() {
    for (int i = 0; i < listItems.size(); i++) {
        SavedList savedList = new SavedList();
        RealmResults<FoodList> queryListName = realm.where(FoodList.class)
                .equalTo("listName", listItems.get(i))
                .findAll();
        savedList.setListName(queryListName.get(i).getListName());
        savedList.setRestaurantName(queryListName.get(i).getRestaurantName());
        addElementsToList(savedList, queryListName);
        plateMap.put(i,savedList);
    }
}

private void addElementsToList(SavedList savedList, RealmResults<FoodList> queryListName) {
    for (int j = 0; j < queryListName.size(); j++) {
        savedList.addToList(queryListName.get(j).getFoodPlate());
    }
}

我的领域对象:

public class FoodList extends RealmObject {
    private int id;
    private String foodPlate;
    private String listName;
    private String restaurantName;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFoodPlate() {
        return foodPlate;
    }

    public void setFoodPlate(String foodPlate) {
        this.foodPlate = foodPlate;
    }

    public String getListName() {
        return listName;
    }

    public void setListName(String listName) {
        this.listName = listName;
    }

    public String getRestaurantName() {
        return restaurantName;
    }

    public void setRestaurantName(String restaurantName) {
        this.restaurantName = restaurantName;
    }
}

我不知道会发生什么,我有点沮丧。

1 个答案:

答案 0 :(得分:0)

我终于得到了解决方案,非常简单:

private void saveListInMap() {
    for (int i = 0; i < listItems.size(); i++) {
        SavedList savedList = new SavedList();
        RealmResults<FoodList> queryListName = realm.where(FoodList.class)
                .equalTo("listName", listItems.get(i))
                .findAll();

        addElementsToList(savedList, queryListName);
        plateMap.put(i,savedList);
    }
}

private void addElementsToList(SavedList savedList, RealmResults<FoodList> queryListName) {
    for (int j = 0; j < queryListName.size(); j++) {
        savedList.setListName(queryListName.get(i).getListName());
        savedList.setRestaurantName(queryListName.get(i).getRestaurantName());
        savedList.addToList(queryListName.get(j).getFoodPlate());
    }
}

只需将setter添加到循环中......否则我会尝试访问数组中不存在的位置。

希望我可以帮助解决这个问题的人。