【问题标题】:Accessing null ArrayList from JSON in Main Activity在主活动中从 JSON 访问 null ArrayList
【发布时间】:2019-07-26 23:46:02
【问题描述】:

我知道这个问题已经被问了很多,但我仍然无法弄清楚我的代码是怎么做的。我正在尝试从这段代码中获取值:

 private void getAnswerKey(){
    class GetAnswerKey extends AsyncTask<Void, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            JSON_STRING = s;
            showAnswerKey();
        }

        @Override
        protected String doInBackground(Void... params) {
            RequestHandler rh = new RequestHandler();
            String s = rh.sendGetRequest(Configuration.URL_GET_ANSWER);
            return s;
        }
    }
    GetAnswerKey gA = new GetAnswerKey();
    gA.execute();
}


private void showAnswerKey () {
    correctAnswers = new ArrayList<Integer>();
    JSONObject jsonObject = null;
    try {
        jsonObject = new JSONObject(JSON_STRING);
        JSONArray result = jsonObject.getJSONArray(Configuration.TAG_JSON_ARRAY);

        for (int i = 0; i < result.length(); i++) {
            JSONObject jo = result.getJSONObject(i);
            int answerKey = jo.getInt(Configuration.TAG_ANSWERKEY);
            correctAnswers.add(answerKey);
            System.out.println((i + 1) + "a. " + options[correctAnswers.get(i)]); //data are there
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
}

如何在我的主要活动中访问 correctAnswers arrayList? correctAnswers 在这段代码中本身不是空的,但是当我尝试用其他方法访问它时,它是空的。我试过将数据传递给其他方法。仍然返回 null。我将arrayList 作为全局变量。有什么想法吗??

【问题讨论】:

  • 你能提供完整的代码吗?您似乎正在尝试在计算之前访问 correctAnswers
  • @suvojit_007 我在onCreate中声明了,就是这样。
  • 发布崩溃日志。
  • @KishoreJethava 没有崩溃!问题是当我尝试以不同的方法访问它时,arraylist 为空
  • 在你得到 null correctAnswers 或完整代码的地方发布代码

标签: android json arraylist


【解决方案1】:

就像 suvojit_007 可能正确假设的那样,您可能正在尝试在填充数组列表之前访问它..

您可以使用接口。创建你的界面...

public interface ResponseInterface {
    public void getResponse(String data);
}

在您的AsyncTask 中声明您的界面

private ResponseInterface responseInterface;

AsyncTask内创建一个构造函数,以接口为参数

public GetAnswerKey(ResponseInterface responseInterface) {
     this.responseInterface = responseInterface;
 }

在你的onPostExecute内,调用接口

@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);
    responseInterface.getResponse(s);
}

...最后在执行AsyncTask 时,执行此操作...

new GetAnswerKey(new ResponseInterface() {
        @Override
        public void getResponse(String data) {
           // do whatever you want with your answers here.
           // Also, whatever function that is accessing the 
           // arrayList, call it here, that way you avoid any 
           // possibility of the arrayList being null
        }
}).execute();

【讨论】:

  • 如果我想从 onCreate 访问它怎么办??
  • 要从 onCreate 访问它,我建议您在 onCreate 中执行 AsyncTask(就像我在上面显示的那样),而不是在您的 getAnswerKey() 函数中执行它...但是如果还有其他需要使用结果或访问 NonNull 数组列表的函数,从 getResponse(String data) 函数中调用它们
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多