简体   繁体   English

为什么标签不显示我的数组元素?

[英]why does not the Tag show my the array element?

it gives me a bunch of errors in the logcat.它在 logcat 中给了我一堆错误。 but if I delete Log.d("intheclass", questionsArray.get(0));但是如果我删除Log.d("intheclass", questionsArray.get(0)); it shows no errors and app launches.它显示没有错误并且应用程序启动。 with that code, app doesn't launch.使用该代码,应用程序无法启动。 I have tried to LOG the response itself, it works.我试图记录响应本身,它有效。 Absolutely have no idea why this array doesn't work.完全不知道为什么这个数组不起作用。

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;
    }
}

it's empty by the time you try to access.当您尝试访问时,它是空的。 When you call getQuestions() it's going to another worker thread to add the values in the questionsArray but your log is on main thread and is in sequence call so it won't work and will give crash.当您调用getQuestions()时,它会转到另一个工作线程来添加questionsArray中的值,但您的日志在主线程上并且是按顺序调用的,因此它不会工作并且会崩溃。

Possible solution.可能的解决方案。

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) {

                }
            });
 }
}

and call this from onRepsonse() after successful api call.并在成功 api 调用后从 onRepsonse() 调用它。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的数组中的每个元素都以“null”开头? - Why does every element in my array start with "null"? 为什么我的 findMax 方法给我的是数组的第一个元素而不是最大元素的结果? - Why does my findMax method gives me the result of the first element of an array instead of the maximum element? 为什么我的JFrame不显示我的标签 - Why does my JFrame not show my label 为什么我的程序只显示我的数组之一<string>我的所有 hashmap 数据中的食物而不是实际值?</string> - Why does my program only show one of my Array <String> Food in all of my hashmap data instead of the actual values? 当我更改数组的元素时,为什么我的方法不返回false? - Why does my method not return false when I change the array's element? 为什么我的水印没有在Java的中心显示? - Why my Watermark does not show in center in java? 为什么我的相框没有任何显示? - Why does my frame show nothing? 为什么minifyEnabled会显示我的大部分代码? - Why does minifyEnabled show most of my code? 为什么println不显示我的数组 - Why println doesn't show my array 为什么我的servlet stacktrace为我的类显示“Unknown Source”? - Why does my servlet stacktrace show “Unknown Source” for my classes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM