使用Achartengine绘制

使用Achartengine绘制

我正在为Android使用achartengine绘制加速度计数据。
但是活动一开始,应用程序就会挂起并过一会儿显示ANR。
请有人告诉我我要去哪里错了。
这是我的代码。

    public class TestButtonActivity extends Activity implements
    SensorEventListener{

private LinearLayout lyGforce;
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private TimeSeries timeSeriesGforcex;
private TimeSeries timeSeriesGforcey;
private TimeSeries timeSeriesGforcez;
private XYMultipleSeriesDataset datasetGforce;
private XYMultipleSeriesRenderer rendererGforce;
private XYSeriesRenderer rendererSeriesGforce;
private XYSeriesRenderer rendererSeriesGforcey;
private XYSeriesRenderer rendererSeriesGforcez;
private GraphicalView viewGforce;
Handler h;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_test_button);
    Log.e("", "deepak");
    h = new Handler();
    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mAccelerometer = mSensorManager
            .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mSensorManager.registerListener(this, mAccelerometer,
    SensorManager.SENSOR_DELAY_UI);

    datasetGforce = new XYMultipleSeriesDataset();
    rendererGforce = new XYMultipleSeriesRenderer();
    rendererGforce.setApplyBackgroundColor(true);
    rendererGforce.setBackgroundColor(Color.argb(100, 50, 50, 50));
    rendererGforce.setAxisTitleTextSize(16);
    rendererGforce.setChartTitleTextSize(20);
    rendererGforce.setLabelsTextSize(15);
    rendererGforce.setLegendTextSize(15);
    rendererGforce.setMargins(new int[] { 20, 30, 15, 20 });
    // rendererGforce.setZoomButtonsVisible(true);

    rendererGforce.setPointSize(10);
    rendererGforce.setShowGrid(true);
    rendererGforce.setGridColor(Color.WHITE);

    rendererSeriesGforce = new XYSeriesRenderer();
    rendererSeriesGforce.setColor(Color.GREEN);
    rendererSeriesGforce.setLineWidth(2);

    rendererSeriesGforcey = new XYSeriesRenderer();
    rendererSeriesGforcey.setColor(Color.RED);
    rendererSeriesGforcey.setLineWidth(2);

    rendererSeriesGforcez = new XYSeriesRenderer();
    rendererSeriesGforcez.setColor(Color.BLUE);
    rendererSeriesGforcez.setLineWidth(2);

    rendererGforce.addSeriesRenderer(rendererSeriesGforce);
    rendererGforce.addSeriesRenderer(rendererSeriesGforcey);
    rendererGforce.addSeriesRenderer(rendererSeriesGforcez);

    timeSeriesGforcex = new TimeSeries("x");
    timeSeriesGforcey = new TimeSeries("y");
    timeSeriesGforcez = new TimeSeries("z");

    datasetGforce.addSeries(timeSeriesGforcex);
    datasetGforce.addSeries(timeSeriesGforcey);
    datasetGforce.addSeries(timeSeriesGforcez);

    viewGforce = ChartFactory.getTimeChartView(this, datasetGforce,
            rendererGforce, "");
    viewGforce.refreshDrawableState();
    viewGforce.repaint();
    lyGforce = (LinearLayout) findViewById(R.id.graph_view_container);
    lyGforce.addView(viewGforce);
    viewGforce.refreshDrawableState();
    viewGforce.repaint();

}

@Override
protected void onResume() {
    super.onResume();

}

@Override
protected void onPause() {
    Log.d("onPause", "onPause");
    mSensorManager.unregisterListener(this);
    super.onPause();
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {


}

@Override
public void onSensorChanged(SensorEvent event) {
    float x = event.values[0];
    float y = event.values[1];
    float z = event.values[2];
    Log.d("", "" + x + "" + y + "" + z);

    timeSeriesGforcex.add(new Date(), x);
    timeSeriesGforcey.add(new Date(), y);
    timeSeriesGforcez.add(new Date(), z);

    rendererGforce.setXAxisMax(rendererGforce.getXAxisMax() + 100);
    rendererGforce.setXAxisMin(rendererGforce.getXAxisMin() + 30);
    viewGforce.repaint();
}}


这就是logcat所说的:

05-10 11:47:16.765: I/dalvikvm(3046): threadid=3: reacting to signal 3
05-10 11:47:17.016: D/dalvikvm(3046): JIT unchain all for threadid=1
05-10 11:47:17.766: W/dalvikvm(3046): threadid=3: spin on suspend #1 threadid=1 (pcf=0)
05-10 11:47:18.517: W/dalvikvm(3046): threadid=3: spin on suspend #2 threadid=1 (pcf=0)
05-10 11:47:18.517: I/dalvikvm(3046): "Signal Catcher" daemon prio=5 tid=3 RUNNABLE
05-10 11:47:18.517: I/dalvikvm(3046):   | group="system" sCount=0 dsCount=0       obj=0x418cd468 self=0x5adad250
05-10 11:47:18.517: I/dalvikvm(3046):   | sysTid=3050 nice=0 sched=0/0 cgrp=apps handle=1524468640
05-10 11:47:18.517: I/dalvikvm(3046):   | state=R schedstat=( 0 0 0 ) utm=0 stm=0 core=1
05-10 11:47:18.517: I/dalvikvm(3046):   at dalvik.system.NativeStart.run(Native Method)
05-10 11:47:18.517: I/dalvikvm(3046): "main" prio=5 tid=1 RUNNABLE JIT
05-10 11:47:18.517: I/dalvikvm(3046):   | group="main" sCount=1 dsCount=0 obj=0x41630728 self=0x4161a658
05-10 11:47:18.517: I/dalvikvm(3046):   | sysTid=3046 nice=0 sched=0/0 cgrp=apps handle=1074864124
05-10 11:47:18.517: I/dalvikvm(3046):   | state=R schedstat=( 0 0 0 ) utm=2175 stm=26 core=0
05-10 11:47:18.517: I/dalvikvm(3046):   at org.achartengine.chart.TimeChart.getXLabels(TimeChart.java:~190)
05-10 11:47:18.517: I/dalvikvm(3046):   at org.achartengine.chart.XYChart.draw(XYChart.java:314)
05-10 11:47:18.517: I/dalvikvm(3046):   at org.achartengine.GraphicalView.onDraw(GraphicalView.java:166)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.View.draw(View.java:14126)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.View.getDisplayList(View.java:13020)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.View.getDisplayList(View.java:13062)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.View.draw(View.java:13839)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.ViewGroup.drawChild(ViewGroup.java:3086)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2923)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.View.getDisplayList(View.java:13015)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.View.getDisplayList(View.java:13062)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.View.draw(View.java:13839)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.ViewGroup.drawChild(ViewGroup.java:3086)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2923)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.View.getDisplayList(View.java:13015)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.View.getDisplayList(View.java:13062)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.View.draw(View.java:13839)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.ViewGroup.drawChild(ViewGroup.java:3086)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2923)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.View.getDisplayList(View.java:13015)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.View.getDisplayList(View.java:13062)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.View.draw(View.java:13839)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.ViewGroup.drawChild(ViewGroup.java:3086)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2923)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.View.draw(View.java:14129)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.widget.FrameLayout.draw(FrameLayout.java:471)
05-10 11:47:18.517: I/dalvikvm(3046):   at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2231)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.View.getDisplayList(View.java:13020)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.View.getDisplayList(View.java:13062)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1411)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1359)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.ViewRootImpl.draw(ViewRootImpl.java:2377)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2249)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1882)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1009)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5508)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.Choreographer.doCallbacks(Choreographer.java:562)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.Choreographer.doFrame(Choreographer.java:532)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.os.Handler.handleCallback(Handler.java:730)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.os.Handler.dispatchMessage(Handler.java:92)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.os.Looper.loop(Looper.java:213)
05-10 11:47:18.517: I/dalvikvm(3046):   at android.app.ActivityThread.main(ActivityThread.java:5225)
05-10 11:47:18.517: I/dalvikvm(3046):   at java.lang.reflect.Method.invokeNative(Native Method)
05-10 11:47:18.517: I/dalvikvm(3046):   at java.lang.reflect.Method.invoke(Method.java:525)
05-10 11:47:18.517: I/dalvikvm(3046):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
05-10 11:47:18.517: I/dalvikvm(3046):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-10 11:47:18.517: I/dalvikvm(3046):   at dalvik.system.NativeStart.main(Native Method)
05-10 11:47:19.268: W/dalvikvm(3046): threadid=3: spin on suspend #3 threadid=1 (pcf=0)
05-10 11:47:19.268: I/dalvikvm(3046): "Signal Catcher" daemon prio=5 tid=3 RUNNABLE
05-10 11:47:19.268: I/dalvikvm(3046):   | group="system" sCount=0 dsCount=0 obj=0x418cd468 self=0x5adad250
05-10 11:47:19.268: I/dalvikvm(3046):   | sysTid=3050 nice=0 sched=0/0 cgrp=apps handle=1524468640
05-10 11:47:19.268: I/dalvikvm(3046):   | state=R schedstat=( 0 0 0 ) utm=0 stm=0 core=1
05-10 11:47:19.268: I/dalvikvm(3046):   at dalvik.system.NativeStart.run(Native Method)
05-10 11:47:19.268: I/dalvikvm(3046): "main" prio=5 tid=1 RUNNABLE JIT
05-10 11:47:19.268: I/dalvikvm(3046):   | group="main" sCount=1 dsCount=0 obj=0x41630728 self=0x4161a658
05-10 11:47:19.268: I/dalvikvm(3046):   | sysTid=3046 nice=0 sched=0/0 cgrp=apps handle=1074864124
05-10 11:47:19.268: I/dalvikvm(3046):   | state=R schedstat=( 0 0 0 ) utm=2246 stm=26 core=0
05-10 11:47:19.268: I/dalvikvm(3046):   at org.achartengine.chart.TimeChart.getXLabels(TimeChart.java:~190)
05-10 11:47:19.268: I/dalvikvm(3046):   at org.achartengine.chart.XYChart.draw(XYChart.java:314)
05-10 11:47:19.268: I/dalvikvm(3046):   at org.achartengine.GraphicalView.onDraw(GraphicalView.java:166)
05-10 11:47:19.268: I/dalvikvm(3046):   at android.view.View.draw(View.java:14126)
05-10 11:47:19.268: I/dalvikvm(3046):   at android.view.View.getDisplayList(View.java:13020)
05-10 11:47:19.268: I/dalvikvm(3046):   at android.view.View.getDisplayList(View.java:13062)
05-10 11:47:19.268: I/dalvikvm(3046):   at android.view.View.draw(View.java:13839)
05-10 11:47:19.268: I/dalvikvm(3046):   at android.view.ViewGroup.drawChild(ViewGroup.java:3086)
05-10 11:47:19.268: I/dalvikvm(3046):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2923)
05-10 11:47:19.268: I/dalvikvm(3046):   at android.view.View.getDisplayList(View.java:13015)

最佳答案

终于自己找到了。

    rendererGforce.setShowGrid(true);


罪魁祸首是onCreate()中的此语句。
将其从onCreate()中注释掉,并在viewGforce.repaint()之前添加到onSensorChanged();它会完美地工作。

确保在onPause()中将其设置回false。

可能有一天可以节省某人的时间。

关于android - 使用Achartengine绘制显示ANR的加速度计数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23577917/

10-10 04:55