我正在开发一个Android项目。我正在创建一个图形并用json内容填充图形。
我的问题是我一直收到这个错误,我不知道为什么。 java.lang.NegativeArraySizeException:-2
My Log.v显示数组的内容。所以它不是空的。也许我错过了什么。
我浏览其余的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 :(得分:0)
我发现了错误。这是我的图表库。
我正在使用MPAndroidChart库,您需要对数据进行排序。我的后端被排序了desc。我不得不将其更改为发布日期(asc)。
它与此问题有关。 NegativeArraySizeException adding Scatter Data to a CombinedChart
我希望这有助于其他人。