2016-12-19 15 views
-1

私はAndroidプロジェクトで作業しています。私はグラフを作成し、グラフにjsonの内容を入力しています。

私の問題は、私はこのエラーが発生し続けていると私はなぜわからないです。 java.lang.NegativeArraySizeException:-2java.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は、それがジュースかもしれないので、私は初心者です

[{"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"}, 

をlog.v簡単な間違いです。
ありがとうございます

+0

"finalJson"の値を投稿できますか?例外の罫線番号 – NehaK

+2

コードのどの行にNegativeArraySizeExceptionが発生しましたか? – Fartab

+0

私はfinalJsonのログアウトを掲載しました。私はすべての価値を得ています。 –

答えて

0

エラーが見つかりました。それは私のグラフライブラリでした。

私はMPAndroidChartライブラリを使用しており、データをソートする必要があります。バックエンドをソートしました。投稿日(asc)に変更する必要がありました。

この問題に関連しています。 NegativeArraySizeException adding Scatter Data to a CombinedChart

私はこれが他の人に役立つことを望みます。

+0

悲しいことに私は同じライブラリを使用していません。良い処分をありがとう。 – statosdotcom

関連する問題