2016-12-19 69 views
-1

我在Android項目。我正在創建一個圖並用json內容填充圖。

我的問題是,我不斷收到此錯誤,我不知道爲什麼。 java.lang.NegativeArraySizeException:-2不斷收到java.lang.NegativeArraySizeException:-2,不知道爲什麼

我Log.v示出了陣列的內容。所以它不是空的。也許我錯過了一些東西。

我去通過REST API,並添加一切到ArrayList resModelList。 在onPostExecute中,我想將我的y軸值添加到此數組列表yVals1。 這是我得到我的錯誤。 (java.lang.NegativeArraySizeException:-2)

如果我添加這樣的價值觀,我沒有得到任何錯誤。

yVals1 = new ArrayList<Entry>(); 
    yVals1.add(new Entry(1451606400, 10)); 
    yVals1.add(new Entry(1454284800, 20)); 
    yVals1.add(new Entry(1456790400, 30)); 
    yVals1.add(new Entry(1459468800, 50)); 

我的代碼
全局變量

ArrayList<ResultModel> resModelList; 
ArrayList<Entry> yVals1; 

解析JSON

//getResult 
public class JSONTask extends AsyncTask<String, String, List<ResultModel>> { 

    @Override 
    protected List<ResultModel> doInBackground(String... params) { 
     BufferedReader reader = null; 
     HttpURLConnection connection = null; 
     try { 
      URL url = new URL(params[0]); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.setRequestProperty("Cookie",session_name+"="+session_id); 
      connection.setRequestProperty("X-CSRF-Token", token); 
      //connection.setRequestProperty("Accept-Encoding", "identity"); 
      connection.connect(); 

      int length = connection.getContentLength(); 

      InputStream stream = connection.getInputStream(); 
      reader = new BufferedReader(new InputStreamReader(stream)); 

      String line = ""; 
      StringBuffer buffer = new StringBuffer(); 
      while ((line = reader.readLine()) != null){ 
       buffer.append(line); 
      } 

      Log.v("TESt", " " + length); 

      String finalJson = buffer.toString(); 
      JSONArray parentArray = new JSONArray(finalJson); 
      resModelList = new ArrayList<>(); 

      for(int i=0; i<parentArray.length(); i++){ 
       JSONObject finalObject = parentArray.getJSONObject(i); 
       ResultModel resModel = new ResultModel(); 
       resModel.setPost_date(finalObject.getString("post_date")); 
       resModel.setHow_much_has_ocd(finalObject.getString("how_much_has_ocd")); 
       resModelList.add(resModel); 
      } 
      return resModelList; 

     }catch (MalformedURLException e){ 
      e.printStackTrace(); 
     }catch (IOException e){ 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } finally { 
      if(connection != null) { 
       connection.disconnect(); 
      } 
      try { 
       if(reader != null){ 
        reader.close(); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(List<ResultModel> result) { 
     super.onPostExecute(result); 
     if(!resModelList.isEmpty()){ 
//here is where i get my errors 
      yVals1 = new ArrayList<Entry>(); 
      for (ResultModel ocd : resModelList){ 
       int score = Integer.parseInt(ocd.getHow_much_has_ocd()); 
       int timeStamp = Integer.parseInt(ocd.getPost_date()); 
//I get these log values 
       Log.v("Score: ", " " + score + " Timestamp: " + timeStamp); 
       yVals1.add(new Entry(timeStamp, score)); 
      } 
      graph(); 
      Log.v("Not Empty list", ""); 
     }else { 
      Log.v("Empty list", ""); 
     } 
    } 
} 

finalJson log.v

[{"post_date":"1481895820","did_you_avoid":"25","how_much_has_ocd":"81","how_would_you_rate":"82","overall_how_would":"35","were_there_any_distressing":"0","uid":"2"},{"post_date":"1481723564","did_you_avoid":"13","how_much_has_ocd":"10","how_would_you_rate":"13","overall_how_would":"16","were_there_any_distressing":"0","uid":"2"},{"post_date":"1481723488","did_you_avoid":"28","how_much_has_ocd":"56","how_would_you_rate":"75","overall_how_would":"32","were_there_any_distressing":"0","uid":"2"},{"post_date":"1481537274","did_you_avoid":"53","how_much_has_ocd":"59","how_would_you_rate":"15","overall_how_would":"71","were_there_any_distressing":"1","uid":"2"},{"post_date":"1481295470","did_you_avoid":"67","how_much_has_ocd":"64","how_would_you_rate":"66","overall_how_would":"57","were_there_any_distressing":"0","uid":"2"},{"post_date":"1481097609","did_you_avoid":"72","how_much_has_ocd":"85","how_would_you_rate":"62","overall_how_would":"64","were_there_any_distressing":"0","uid":"2"},{"post_date":"1480673252","did_you_avoid":"33","how_much_has_ocd":"69","how_would_you_rate":"84","overall_how_would":"37","were_there_any_distressing":"1","uid":"2"}, 

我是初學者,所以可能強制不是一個簡單的錯誤。
在此先感謝

+0

你可以發佈「finalJson」的價值嗎?和異常的線程數 – NehaK

+2

NegativeArraySizeException發生在您的代碼行中? – Fartab

+0

我發佈了finalJson註銷。我獲得了所有的價值。 –

回答

0

我發現的錯誤。這是我的圖形庫。

我使用MPAndroidChart庫,你需要對數據進行排序。我的後端是按照desc排序的。我不得不將它改爲Post date(asc)。

它與這個問題有關。 NegativeArraySizeException adding Scatter Data to a CombinedChart

我希望這可以幫助別人。

+0

可悲的是我沒有使用相同的庫。謝謝你的好意。 – statosdotcom

相關問題