我是 android 新手,仍然只是在玩它。但是,最近我遇到了一个问题,我被卡住了。我正在尝试从加速度计中获取数据,然后使用 achartengine 实时绘制它。我找到了一个教程,它首先保存来自加速度计的数据,然后绘制它。即使我可以在模拟器上运行它,但一旦我将它部署到运行 jelly bean 的 Nexus 4 上,应用程序就会崩溃。我似乎无法弄清楚为什么。你有什么建议吗?
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<AccelemoterData>();
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<AccelemoterData>();
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;
}
}
}
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());
}
}