0

I am not sure if I am missing something conceptually or if there is an error in one of my methods, but I have debugged this activity for quite some time and I must be overlooking something huge. At the end of my onCreate, I instantiate an ASyncTask to populate an ArrayList of data from a GET, and then in the onPostExecute I reset the GraphView data with the new data. My onCreate is below:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cholestoral);
    graph = (GraphView) findViewById(R.id.graph);
    //dummy data for now, TODO: adjust dates to fit in graphical format
    HDL_series = new LineGraphSeries<>(new DataPoint[] {
            new DataPoint(0, 2),
            new DataPoint(1, 1),
            new DataPoint(2, 5),
            new DataPoint(3, 3),
            new DataPoint(4, 6),
    });
    HDL_series.setColor(Color.RED);
    HDL_series.setTitle("HDL");
    LDL_series = new LineGraphSeries<>(new DataPoint[] {
            new DataPoint(0, 7),
            new DataPoint(1, 8),
            new DataPoint(2, 5),
            new DataPoint(3, 8),
            new DataPoint(4, 7),
    });
    LDL_series.setColor(Color.CYAN);
    LDL_series.setTitle("LDL");
    graph.addSeries(HDL_series);
    graph.addSeries(LDL_series);
    //empty arraylist on page load... to be filled in ASyncTask below
    new ListCholestoralAPI().execute("http://m-health.cse.nd.edu:8000/phrService-0.0.1-SNAPSHOT/chol/chol/");

The doInBackground correctly populates my list of information, and it gets sent to the onPostExecute which must be the section that keeps crashing the application. My onPostExecute simply calls a function in my activity called initializeGraph().

public void initializeGraph() {
    //TODO: initialize graph by setting labels, axes, etc...
    HDL_series.resetData(generateDataPoints());
}

public DataPoint[] generateDataPoints() {
    dataPointArray = null;
    dataPointArray = new DataPoint[30];
    for (int i = 0; i < cholesterolInformationList.size(); i++) {
        double hdl = cholesterolInformationList.get(i).getHdl();
        dataPointArray[i] = new DataPoint(i, hdl);
    }
    //dataPointArray = sortDataPointArray(dataPointArray);
    return dataPointArray;
}

Note: If anything jumps out at you as conceptually flawed here, please let me know! I have spent enormous amounts of time on a fairly simple task.

HexAndBugs
  • 5,549
  • 2
  • 27
  • 36
Jack Ryan
  • 1,287
  • 12
  • 26
  • 2
    Are you getting any particular exception logged? Does your data have more than 30 elements? If so, it will crash. It seems odd to allocate a new 30 element array rather than doing `dataPointArray = new DataPoint[cholesterolInformationList.size()];` Also, you don't need to set `dataPointArray = null` immediately before you allocate the new array (although it won't be the cause of your problem). – HexAndBugs Jul 12 '15 at 23:43
  • It would be helpful if you post the point of crash via Logcat. what chain of events caused it and the related code. Because the posted code looks clean to me. And as @HexAndBugs mentioned above, do implement those changes. – Paras Jul 16 '15 at 22:24

0 Answers0