Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.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
Android 返回截击onRsponse外的修改arrayList_Android_Arraylist_Android Volley - Fatal编程技术网

Android 返回截击onRsponse外的修改arrayList

Android 返回截击onRsponse外的修改arrayList,android,arraylist,android-volley,Android,Arraylist,Android Volley,我需要在volley onResponse方法之外访问此arrayList。但它正在返回IndexOutOfBoundsException。 public ArrayList<QuestionAnswer> arrayList; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); arr

我需要在volley onResponse方法之外访问此arrayList。但它正在返回IndexOutOfBoundsException。

public ArrayList<QuestionAnswer> arrayList;

    protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                arrayList = new ArrayList<>();

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, getTalkById, new JSONObject(params),
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                JSONObject data = response.optJSONObject("data");
                                JSONArray highSchoolQuesAns = data.optJSONArray("questions").optJSONObject(0).optJSONArray("answers");

                                for (int i = 0; i < highSchoolQuesAns.length(); i++) {

                                    String question = highSchoolQuesAns.optJSONObject(i).optString("question");
                                    String answer = highSchoolQuesAns.optJSONObject(i).optString("answer");

                                    arrayList.add(new QuestionAnswer(question, answer)); 
    // adding new QuestionAnswer 
                                }
Log.v("test",arrayList.get(0).question); // value is available here inside on response. And getting question of first arrayList element.
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                VolleyLog.d("Error", "Error: " + error.getMessage());
                            }
                        }
                );
                request.setRetryPolicy(new DefaultRetryPolicy(
                        VolleyApplication.TIMEOUT,
                        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
                VolleyApplication.getInstance().getRequestQueue().add(request);


                Log.v("test",arrayList.get(0).question); //java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
//This is where the issue is. AND I REALLY NEED TO access this arraylist outside of volley onResponse method 

        }
公共ArrayList ArrayList;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
arrayList=新的arrayList();
JsonObjectRequest=newJSONObjectRequest(request.Method.POST、getTalkById、newJSONObject(params),
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
JSONObject data=response.optJSONObject(“数据”);
JSONArray Highschool Quesans=data.optJSONArray(“问题”).optJSONObject(0).optJSONArray(“答案”);
对于(int i=0;i

正如您所见,我正在尝试登录两个地方。来自onResponse的日志确实返回了一些内容,但在onResponse之外,它只会给出IndexOutOfBoundException。我做了很多调试,并意识到onResponse arrayList的外部是空的(我正在尝试访问一个不存在的空元素)。在onResponse内部修改的ArrayList在外部无法访问。即使它是一个全局/类级别的变量

我建议您在成功响应截击后使用单独的方法,如下所示

public ArrayList<QuestionAnswer> arrayList;

    protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                arrayList = new ArrayList<>();

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, getTalkById, new JSONObject(params),
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                JSONObject data = response.optJSONObject("data");
                                JSONArray highSchoolQuesAns = data.optJSONArray("questions").optJSONObject(0).optJSONArray("answers");

                                for (int i = 0; i < highSchoolQuesAns.length(); i++) {

                                    String question = highSchoolQuesAns.optJSONObject(i).optString("question");
                                    String answer = highSchoolQuesAns.optJSONObject(i).optString("answer");

                                    arrayList.add(new QuestionAnswer(question, answer)); 
    // adding new QuestionAnswer 
                                }
                                GetArrayValue();
Log.v("test",arrayList.get(0).question); // value is available here inside on response. And getting question of first arrayList element.
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                VolleyLog.d("Error", "Error: " + error.getMessage());
                            }
                        }
                );
                request.setRetryPolicy(new DefaultRetryPolicy(
                        VolleyApplication.TIMEOUT,
                        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
                VolleyApplication.getInstance().getRequestQueue().add(request);

        }

        private void GetArrayValue(){
         Log.v("test",arrayList.get(0).question); 
         }
公共ArrayList ArrayList;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
arrayList=新的arrayList();
JsonObjectRequest=newJSONObjectRequest(request.Method.POST、getTalkById、newJSONObject(params),
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
JSONObject data=response.optJSONObject(“数据”);
JSONArray Highschool Quesans=data.optJSONArray(“问题”).optJSONObject(0).optJSONArray(“答案”);
对于(int i=0;i
我建议您在截击成功后使用单独的方法,如下所示

public ArrayList<QuestionAnswer> arrayList;

    protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                arrayList = new ArrayList<>();

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, getTalkById, new JSONObject(params),
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                JSONObject data = response.optJSONObject("data");
                                JSONArray highSchoolQuesAns = data.optJSONArray("questions").optJSONObject(0).optJSONArray("answers");

                                for (int i = 0; i < highSchoolQuesAns.length(); i++) {

                                    String question = highSchoolQuesAns.optJSONObject(i).optString("question");
                                    String answer = highSchoolQuesAns.optJSONObject(i).optString("answer");

                                    arrayList.add(new QuestionAnswer(question, answer)); 
    // adding new QuestionAnswer 
                                }
                                GetArrayValue();
Log.v("test",arrayList.get(0).question); // value is available here inside on response. And getting question of first arrayList element.
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                VolleyLog.d("Error", "Error: " + error.getMessage());
                            }
                        }
                );
                request.setRetryPolicy(new DefaultRetryPolicy(
                        VolleyApplication.TIMEOUT,
                        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
                VolleyApplication.getInstance().getRequestQueue().add(request);

        }

        private void GetArrayValue(){
         Log.v("test",arrayList.get(0).question); 
         }
公共ArrayList ArrayList;
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
arrayList=新的arrayList();
JsonObjectRequest=newJSONObjectRequest(request.Method.POST、getTalkById、newJSONObject(params),
新的Response.Listener(){
@凌驾
公共响应(JSO)