Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.lang.IndexOutOfBoundsException:索引500无效,在for循环内部使用改装回调时大小为1_Java_Android_Callback_Retrofit - Fatal编程技术网

java.lang.IndexOutOfBoundsException:索引500无效,在for循环内部使用改装回调时大小为1

java.lang.IndexOutOfBoundsException:索引500无效,在for循环内部使用改装回调时大小为1,java,android,callback,retrofit,Java,Android,Callback,Retrofit,我正在尝试使用改型从api获取数据,并使用ArrayList存储数据,从api获取数据后,ArrayList的大小是500。在尝试从ArrayList记录项目时,我的应用程序崩溃,我将 错误java.lang.IndexOutOfBoundsException:索引500无效,大小为 一, 我认为问题是由变量int I引起的。请纠正我的错误,并指导我如何解决这个问题 private int i = 0; private void get() { // size of newList is

我正在尝试使用改型从api获取数据,并使用ArrayList存储数据,从api获取数据后,ArrayList的大小是500。在尝试从ArrayList记录项目时,我的应用程序崩溃,我将

错误java.lang.IndexOutOfBoundsException:索引500无效,大小为 一,

我认为问题是由变量int I引起的。请纠正我的错误,并指导我如何解决这个问题

private int i = 0;
private void get() {
    // size of newList is 500
    for ( i = 0; i < newList.size(); i++) { 
        apiService.getNewStories(newStoryIdList.get(i), new Callback<Story>() {
            @Override
            public void success(Story story, Response response) {
                listStory.add(story);
                Log.d("newListItems" ,+listStory.get(i).toString()); // getting error java.lang.IndexOutOfBoundsException: Invalid index 500, size is 1
            }

            @Override
            public void failure(RetrofitError error) {
            }
        });
    }
}

newList的大小可能是500,但newStoryIdList呢?

我认为问题在于这一行: apiService.getNewStoriesnewStoryIdList.geti,新回调{


如果没有apiService.getNewStories的定义,我可以假设您需要使用apiService.getNewStoriesnewStoryIdList,新回调{?

我想问题是,当回调到达时,您的变量I的值为500,因为for循环将在本地增加它

回调后会返回,并向listStory添加1个story,此时我的值仍然为500,您在列表上调用listStory.geti.toString,该列表当时只有1个值

编辑:解决方案: 删除调试输出,或写入:

Log.d("newListItems" ,story.toString());

通过检查newList的大小可以解决我的问题。如果newList的大小大于0,则将执行for循环,否则不会执行

private int i = 0;
private void get() {
    if(newList.size() > 0) {
       for ( i = 0; i < newList.size(); i++) { 
          apiService.getNewStories(newStoryIdList.get(i), new  Callback<Story>() {
                @Override
                public void success(Story story, Response response) {
                  listStory.add(story);
                  Log.d("newListItems" ,+listStory.get(i).toString()); 
        }

        @Override
        public void failure(RetrofitError error) {
        }
    });
}
}
}

您确定新列表的大小是500吗?错误告诉我们大小为1。您是否能够发布显示如何创建和填充newList的代码?错误可能位于newStoryIdList.geti上。你确定这个列表有500个iten吗?你有三个列表-newlist、newStoryIdList、listStory,并且你对它们都使用索引i。你确定这就是你想要做的吗?啊,是的,我没注意到在那行中使用了不同的列表。OP,这是您的问题,您可能使用了错误的列表,您想在使用listStory的地方使用newList,或者您忘记填充listStory是的,这就是问题所在,请帮助我解决此问题。我应该使用另一个变量而不是I,或者使用iyou's not have problem,只需删除调试输出,或者只调用story.to字符串就可以解决我的问题,但我希望在成功之外使用listStory,这是可能的。实际上,在将所有项目添加到listStory之后,我想对其进行排序并显示排序后的listStory.apisService是一个具有getNewStories的接口