我是android编程应用程序的新手。我正在尝试使用 achartengine 绘制实时数据。问题是应用程序在一段时间后崩溃。问题是重绘方法并向图形添加新数据,应该是线程问题。如何控制线程,或找到更好的解决方案来绘制图形。如果你能帮助我,那就太好了。
public class Plot2Activity extends Activity {
private static GraphicalView view;
private LineGraph line = new LineGraph();
private static Thread thread;
private static int x=0;
private static boolean mrepainted = false;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view = line.getView(this);
setContentView(view);
thread = new Thread() {
public void run() {
/*for (int i = 0; i < 1000; i++) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
Point p = MockData.getDataFromReceiver(x++); // new data
line.addNewPoints(p); // add it to our graph
view.repaint();
}*/
while (true) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
Point p = MockData.getDataFromReceiver(x++); // new data
line.addNewPoints(p); // add it to our graph
// Log.e("PLOT", "PLOT:"+x); view.repaint();
}
}
};
thread.start();
}
@Override
protected void onStart() {
super.onStart();
//view = line.getView(this);
//setContentView(view);
}
@Override
protected void onResume() {
super.onResume();
}
}
日志猫:
10-08 13:23:22.012:E/AndroidRuntime(9267):致命异常:主要 10-08 13:23:22.012:E/AndroidRuntime(9267):java.util.ConcurrentModificationException 10-08 13:23:22.012: E/AndroidRuntime(9267): 在 java.util.TreeMap$MapIterator.stepForward(TreeMap.java:883) 10-08 13:23:22.012: E/AndroidRuntime(9267): 在 java.util.TreeMap$BoundedMap$BoundedIterator .stepForward(TreeMap.java:1485) 10-08 13:23:22.012: E/AndroidRuntime(9267): 在 java.util.TreeMap$BoundedMap$BoundedEntrySet$1.next(TreeMap.java:1513) 10-08 13: 23:22.012: E/AndroidRuntime(9267): at java.util.TreeMap$BoundedMap$BoundedEntrySet$1.next(TreeMap.java:1511) 10-08 13:23:22.012: E/AndroidRuntime(9267): at org. achartengine.chart.XYChart.draw(XYChart.java:250) 10-08 13:23:22.012: E/AndroidRuntime(9267): at org.achartengine.GraphicalView。onDraw(GraphicalView.java:166) 10-08 13:23:22.012: E/AndroidRuntime(9267): 在 android.view.View.draw(View.java:13458) 10-08 13:23:22.012: E/ AndroidRuntime(9267): 在 android.view.View.draw(View.java:13342) 10-08 13:23:22.012: E/AndroidRuntime(9267): 在 android.view.ViewGroup.drawChild(ViewGroup.java:2929 ) 10-08 13:23:22.012: E/AndroidRuntime(9267): 在 android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799) 10-08 13:23:22.012: E/AndroidRuntime(9267): 在 android .view.View.draw(View.java:13340) 10-08 13:23:22.012: E/AndroidRuntime(9267): 在 android.view.ViewGroup.drawChild(ViewGroup.java:2929) 10-08 13:23 :22.012: E/AndroidRuntime(9267): 在 android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799) 10-08 13:23:22.012: E/AndroidRuntime(9267): 在 android.view.View.draw( View.java:13340) 10-08 13:23:22。012: E/AndroidRuntime(9267): 在 android.view.ViewGroup.drawChild(ViewGroup.java:2929) 10-08 13:23:22.012: E/AndroidRuntime(9267): 在 android.view.ViewGroup.dispatchDraw(ViewGroup .java:2799) 10-08 13:23:22.012: E/AndroidRuntime(9267): 在 android.view.View.draw(View.java:13461) 10-08 13:23:22.012: E/AndroidRuntime(9267) ): 在 android.widget.FrameLayout.draw(FrameLayout.java:467) 10-08 13:23:22.012: E/AndroidRuntime(9267): 在 com.android.internal.policy.impl.PhoneWindow$DecorView.draw( PhoneWindow.java:2183) 10-08 13:23:22.012: E/AndroidRuntime(9267): 在 android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2256) 10-08 13:23:22.012: E/AndroidRuntime( 9267): 在 android.view.ViewRootImpl.draw(ViewRootImpl.java:2151) 10-08 13:23:22.012: E/AndroidRuntime(9267): 在 android.view.ViewRootImpl。performDraw(ViewRootImpl.java:2019) 10-08 13:23:22.012: E/AndroidRuntime(9267): 在 android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1830) 10-08 13:23:22.012: E/ AndroidRuntime(9267): 在 android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998) 10-08 13:23:22.012: E/AndroidRuntime(9267): 在 android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java :4212) 10-08 13:23:22.012: E/AndroidRuntime(9267): 在 android.view.Choreographer$CallbackRecord.run(Choreographer.java:725) 10-08 13:23:22.012: E/AndroidRuntime(9267) ): 在 android.view.Choreographer.doCallbacks(Choreographer.java:555) 10-08 13:23:22.012: E/AndroidRuntime(9267): 在 android.view.Choreographer.doFrame(Choreographer.java:525) 10- 08 13:23:22.012:E/AndroidRuntime(9267):在 android.view。Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711) 10-08 13:23:22.012: E/AndroidRuntime(9267): 在 android.os.Handler.handleCallback(Handler.java:615) 10-08 13:23: 22.012: E/AndroidRuntime(9267): 在 android.os.Handler.dispatchMessage(Handler.java:92) 10-08 13:23:22.012: E/AndroidRuntime(9267): 在 android.os.Looper.loop(Looper .java:137) 10-08 13:23:22.012: E/AndroidRuntime(9267): 在 android.app.ActivityThread.main(ActivityThread.java:4745) 10-08 13:23:22.012: E/AndroidRuntime(9267) ): 在 java.lang.reflect.Method.invokeNative(Native Method) 10-08 13:23:22.012: E/AndroidRuntime(9267): 在 java.lang.reflect.Method.invoke(Method.java:511) 10 -08 13:23:22.012: E/AndroidRuntime(9267): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 10-08 13:23:22.012:E/AndroidRuntime(9267): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 10-08 13:23:22.012: E/AndroidRuntime(9267): 在 dalvik.system.NativeStart.main (本机方法)