【问题标题】:why does not the Tag show my the array element?为什么标签不显示我的数组元素?
【发布时间】:2020-08-12 17:52:45
【问题描述】:

它在 logcat 中给了我一堆错误。但是如果我删除Log.d("intheclass", questionsArray.get(0)); 它显示没有错误并且应用程序启动。使用该代码,应用程序无法启动。我试图记录响应本身,它有效。完全不知道为什么这个数组不起作用。

package com.example.trivia.data;

import android.util.Log;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.example.trivia.controller.AppControl;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

public class QuestionBank {
    private String que1;
    String url = "https://raw.githubusercontent.com/curiousily/simple-quiz/master/script/statements.json";
    ArrayList<String> questionsArray= new ArrayList<String>();



    public List<String> getQuestions(){
        JsonArrayRequest bank = new JsonArrayRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        for(int i=0; i<response.length(); i++){
                            try {
                                que1 = (String) response.getJSONArray(i).get(0);
                                Log.d("queva", String.valueOf(i));
                                questionsArray.add(que1);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                        //if(callback != null) callback.processFinish(questionsArray);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });
        AppControl.getInstance().addToRequestQueue(bank);
        Log.d("intheclass", questionsArray.get(0));
        return questionsArray;
    }
}

【问题讨论】:

  • 可能,如果 questionsArray 是异步填充的,那么当主线程到达 Log.d 语句时,就不会填充了。尽管您可能希望发布您看到的完整错误。

标签: java android arrays


【解决方案1】:

在您尝试访问时它是空的。当您调用 getQuestions() 时,它会转到另一个工作线程来添加 questionsArray 中的值,但是您的日志在主线程上并且是按顺序调用的,所以它不会工作,会崩溃。

可能的解决方案。

public void getQuestions(){
    JsonArrayRequest bank = new JsonArrayRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    for(int i=0; i<response.length(); i++){
                        try {
                            que1 = (String) response.getJSONArray(i).get(0);
                            Log.d("queva", String.valueOf(i));
                            questionsArray.add(que1);
                            doSomething(); //here calling
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    //if(callback != null) callback.processFinish(questionsArray);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
 }
}

并在成功调用 api 后从 onRepsonse() 调用它。

public void doSomething(){
    AppControl.getInstance().addToRequestQueue(bank);
    Log.d("intheclass", questionsArray.get(0));
}

【讨论】:

    猜你喜欢
    • 2018-02-26
    • 1970-01-01
    • 2014-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    • 2021-01-01
    相关资源
    最近更新 更多