使用 volley 库解析 JSON 数据时出现问题

Problem in parsing JSON data using volley library

这是一个从互联网上获取 JSON 数据的简单程序。 answerWithAsyncTask()是一个接口,确保所有下载的数据只有在下载完成后才会添加到questionArrayList

错误:java.lang.IndexOutOfBoundsException:索引:1,大小:0

 private List<Question> questionList;
 protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

//Few findViewbyId's here. Ignoring them

        questionList = new QuestionBank().getQuestions(new answerWithAsyncTask() {
            @Override
            public void asyncMe(ArrayList<Question> questionArrayList) {

                questionTextview.setText(questionArrayList.get(currentQuestionIndex).getQuestionId());
            }
        });
                 updateQuestion();     //This is the newly added line
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.prev_button:
                updateQuestion();
                break;
        }
    }
    private void updateQuestion() {
        String question = questionList.get(1).getQuestionId();
        questionTextview.setText(question);
    }

更新 这是我的 getQuestions 方法。

String url ="https://raw.githubusercontent.com/curiousily/simple-quiz/master/script/statements-data.json";
    private ArrayList<Question> questionArrayList= new ArrayList<>();

    public List<Question> getQuestions (final answerWithAsyncTask callback){
        JsonArrayRequest jsonArrayRequest =new JsonArrayRequest(Request.Method.GET, url, (String) null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        for(int i=0;i<response.length();i++){
                            Question question = new Question();
                            try {
                                question.setQuestionId(response.getJSONArray(i).getString(0));
                                question.setTorF(response.getJSONArray(i).getBoolean(1));
                                questionArrayList.add(question);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                        if(null != callback) callback.asyncMe(questionArrayList);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                });
        AppController.getInstance().addToRequestQueue(jsonArrayRequest);
        return questionArrayList;
    }

这是我的界面 answerWithAsyncTask


public interface answerWithAsyncTask {
    void asyncMe(ArrayList<Question> arrayList);
}

您收到此错误是因为

 private List<Question> questionList;

为空。在您的第一个代码中,您没有收到任何错误,因为您没有调用 updateQuestion() ,它会尝试从空列表中获取数据。错误在第二个代码中,因为它试图访问该空列表。你的

 return questionArrayList;

没有向问题列表返回数据。在这里,您正在尝试进行接口回调,要正确实现它,请查看此答案并相应地更改您的代码,