Android-GraphView中的java.util.ConcurrentModificationException

时间:2017-05-16 19:16:43

标签: android concurrency android-graphview

我正在尝试使用2个图表视图来显示加速度计和陀螺仪数据。图表工作但我得到以下错误,随机发生。

java.util.ConcurrentModificationException
    at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
    at com.jjoe64.graphview.series.BaseSeries$1.next(BaseSeries.java:236)
    at com.jjoe64.graphview.series.BaseSeries$1.next(BaseSeries.java:186)
    at com.jjoe64.graphview.series.LineGraphSeries.draw(LineGraphSeries.java:255)
    at com.jjoe64.graphview.GraphView.drawGraphElements(GraphView.java:301)
    at com.jjoe64.graphview.GraphView.onDraw(GraphView.java:323)
    at android.view.View.draw(View.java:16187)
    at android.view.View.updateDisplayListIfDirty(View.java:15184)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
    at android.view.View.updateDisplayListIfDirty(View.java:15144)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
    at android.view.View.updateDisplayListIfDirty(View.java:15144)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
    at android.view.View.updateDisplayListIfDirty(View.java:15144)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
    at android.view.View.updateDisplayListIfDirty(View.java:15144)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
    at android.view.View.updateDisplayListIfDirty(View.java:15144)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
    at android.view.View.updateDisplayListIfDirty(View.java:15144)
    at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
    at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
    at android.view.View.updateDisplayListIfDirty(View.java:15144)
    at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:282)
    at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:288)
    at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:323)
    at android.view.ViewRootImpl.draw(ViewRootImpl.java:2623)
    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2442)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2075)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6023)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
    at android.view.Choreographer.doCallbacks(Choreographer.java:670)
    at android.view.Choreographer.doFrame(Choreographer.java:606)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5461)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我使用onSensorChanged来收集数据,使用Timer Task绘制图形。

public void onSensorChanged(SensorEvent event) {
    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        synchronized (locka) {
            System.arraycopy(event.values, 0, accSample, 0, 3);
        }
    }
    else if (event.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
        synchronized (lockgy) {
            System.arraycopy(event.values, 0, gyroSample, 0, 3);
        }
    }
}

计时器任务。预定每秒4次。

private class SampleTask extends TimerTask{

    DataPoint ax;
    DataPoint ay;
    DataPoint az;
    DataPoint gx;
    DataPoint gy;
    DataPoint gz;
    final int MAX_DP = 40;
    SampleTask(){
        super();
        timer.start();
    }
    @Override
    public void run() {
        long lTime = timer.getTime();
        double time = ((double)lTime)/1000.0;
        synchronized (locka) {
            ax = new DataPoint(time,accSample[0]);
            ay = new DataPoint(time,accSample[1]);
            az = new DataPoint(time,accSample[2]);
        }
        synchronized (lockgy) {
            gx = new DataPoint(time,gyroSample[0]);
            gy = new DataPoint(time,gyroSample[1]);
            gz = new DataPoint(time,gyroSample[2]);
        }
        accX.appendData(ax,true,MAX_DP);
        accY.appendData(ay,true,MAX_DP);
        accZ.appendData(az,true,MAX_DP);
        gyroX.appendData(gx,true,MAX_DP);
        gyroY.appendData(gy,true,MAX_DP);
        gyroZ.appendData(gz,true,MAX_DP);       
    }
}

图表初始化(在onCreate()内)

    accGraph = (GraphView)findViewById(R.id.acc_graph);
    accGraph.getViewport().setXAxisBoundsManual(true);
    accGraph.getViewport().setMinX(0);
    accGraph.getViewport().setMaxX(10);
    accX= new LineGraphSeries<>();
    accY= new LineGraphSeries<>();
    accZ= new LineGraphSeries<>();
    accX.setColor(Color.RED);
    accY.setColor(Color.GREEN);
    accZ.setColor(Color.BLUE);
    accX.setTitle("X");
    accY.setTitle("Y");
    accZ.setTitle("Z");
    accX.setThickness(LINE_T);
    accY.setThickness(LINE_T);
    accZ.setThickness(LINE_T);
    accGraph.addSeries(accX);
    accGraph.addSeries(accY);
    accGraph.addSeries(accZ);
    accGraph.setTitle("Accel");

    gyroGraph = (GraphView)findViewById(R.id.gyro_graph);
    gyroGraph.getViewport().setXAxisBoundsManual(true);
    gyroGraph.getViewport().setMinX(0);
    gyroGraph.getViewport().setMaxX(10);
    gyroX= new LineGraphSeries<>();
    gyroY= new LineGraphSeries<>();
    gyroZ= new LineGraphSeries<>();
    gyroX.setColor(Color.RED);
    gyroY.setColor(Color.GREEN);
    gyroZ.setColor(Color.BLUE);
    gyroX.setTitle("X");
    gyroY.setTitle("Y");
    gyroZ.setTitle("Z");
    gyroX.setThickness(LINE_T);
    gyroY.setThickness(LINE_T);
    gyroZ.setThickness(LINE_T);
    gyroGraph.addSeries(gyroX);
    gyroGraph.addSeries(gyroY);
    gyroGraph.addSeries(gyroZ);
    gyroGraph.setTitle("Gyro");

1 个答案:

答案 0 :(得分:2)

我找到了解决方案。

使用带有theme_fivethirtyeight()的{​​{1}}代替TimerTask来处理异常。

Runnable