title: Data from accelerometer to achartengine in realtime
body: <p>I'm new to android and still just playing with it. However, recently I ran into a problem and I got stuck. I'm trying to take data from an accelerometer and then plot it using achartengine in real time. I found a tutorial which first saves the data from the accelerometer and then plots it. Even though I can run it on emulator, once I deploy it on my  Nexus 4 running jelly bean, the application crashes. I can't seem to figure out why. Do you have any suggestions?</p>
  
  <pre><code>public class MainActivity extends Activity implements SensorEventListener,OnClickListener {
  
  private Button butStart, butStop;
  private LinearLayout layout;
  private boolean started = false;
  private SensorManager sensorManager;
  
  
  private static GraphicalView view;
  private LineChart line = new LineChart();
  private long t;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
  
      layout = (LinearLayout) findViewById(R.id.chart_container);
      sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
      new ArrayList&lt;AccelemoterData&gt;();
  
  
      butStart = (Button) findViewById(R.id.butStart);
      butStop = (Button) findViewById(R.id.butStop);
  
      butStart.setOnClickListener(this);
      butStop.setOnClickListener(this);
  
      butStart.setEnabled(true);
      butStop.setEnabled(false);
  
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  @Override
  public void onAccuracyChanged(Sensor sensor, int accuracy) {
      // TODO Auto-generated method stub
  
  }
  
  @Override
  public void onSensorChanged(SensorEvent event) {
      if (started) {
  
          double x = event.values[0];
          double y = event.values[1];
          double z = event.values[2];
          long time = System.currentTimeMillis();
          AccelemoterData data = new AccelemoterData(time, x, y, z);
  
          line.addNewData(data, t);
          view.repaint();
  
  
  
  
      }
  }
  
  @Override
  public void onClick(View v) {
      switch (v.getId()) {
      case R.id.butStart:
  
          view = line.getView(this);
          setContentView(view);
          butStop.setEnabled(true);
          butStart.setEnabled(false);
          new ArrayList&lt;AccelemoterData&gt;();
          started = true;
          t = System.currentTimeMillis();
          Sensor accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
          sensorManager.registerListener(this, accelerometer,SensorManager.SENSOR_DELAY_FASTEST);
  
  
          break;
      case R.id.butStop:
          butStop.setEnabled(false);
          butStart.setEnabled(true);
          started = false;
          sensorManager.unregisterListener(this);
          layout.removeAllViews();
  
          break;
      default:
          break;
  
      }
  
  
  
  
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>public class LineChart {
  
  private GraphicalView view;
  
  
  
  private TimeSeries xSeries = new TimeSeries("X");
  private TimeSeries ySeries = new TimeSeries("Y");
  private TimeSeries zSeries = new TimeSeries("Z");
  
  private XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
  
  XYSeriesRenderer xRenderer = new XYSeriesRenderer();
  XYSeriesRenderer yRenderer = new XYSeriesRenderer();
  XYSeriesRenderer zRenderer = new XYSeriesRenderer();
  
  XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
  
  public LineChart() {
  
  
      dataset.addSeries(xSeries);
      dataset.addSeries(ySeries);
      dataset.addSeries(zSeries);
  
  
      xRenderer.setColor(Color.RED);
      xRenderer.setPointStyle(PointStyle.CIRCLE);
      xRenderer.setFillPoints(true);
      xRenderer.setLineWidth(1);
      xRenderer.setDisplayChartValues(false);
  
  
      yRenderer.setColor(Color.GREEN);
      yRenderer.setPointStyle(PointStyle.CIRCLE);
      yRenderer.setFillPoints(true);
      yRenderer.setLineWidth(1);
      yRenderer.setDisplayChartValues(false);
  
  
      zRenderer.setColor(Color.BLUE);
      zRenderer.setPointStyle(PointStyle.CIRCLE);
      zRenderer.setFillPoints(true);
      zRenderer.setLineWidth(1);
      zRenderer.setDisplayChartValues(false);
  
  
      multiRenderer.setXLabels(0);
      multiRenderer.setLabelsColor(Color.RED);
      multiRenderer.setChartTitle("t vs (x,y,z)");
      multiRenderer.setXTitle("Sensor Data");
      multiRenderer.setYTitle("Values of Acceleration");
      multiRenderer.setZoomButtonsVisible(true);
  
      multiRenderer.addSeriesRenderer(xRenderer);
      multiRenderer.addSeriesRenderer(yRenderer);
      multiRenderer.addSeriesRenderer(zRenderer);
  }
  
  public GraphicalView getView(Context context) 
  {
      view =  ChartFactory.getLineChartView(context, dataset, multiRenderer);
      return view;
  }
  
  public void addNewData(AccelemoterData data, long t)
  {
      xSeries.add(data.getTime()-t, data.getX());
      ySeries.add(data.getTime()-t, data.getY());
      zSeries.add(data.getTime()-t, data.getZ());
  
  }
  </code></pre>
  
  <p>}</p>