我有一个班级从我手机的加速度计接收数据。
传感器数据放入queuex, queuey and queuez
。
public class AcceInterface implements SensorEventListener {
private SensorManager sensorManager;
private Sensor accelerometer;
public Queue<Float> queuex;
public Queue<Float> queuey;
public Queue<Float> queuez;
@Override
public void onSensorChanged(SensorEvent event) {
queuex.add(Float.valueOf(event.values[0]));
queuey.add(Float.valueOf(event.values[1]));
queuez.add(Float.valueOf(event.values[2]));
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
在我的mainactivity
中,我从队列中获取加速度计数据并在可运行的线程中绘图。
acce = new AcceInterface();
class Plot implements Runnable
{
Thread mythread ;
Plot()
{
mythread = new Thread(this, "my runnable thread");
System.out.println("my thread created" + mythread);
mythread.start();
}
public void run()
{
while (systemON) {
if (!acce.queuex.isEmpty()) {
mSeriesX.add(x_axis, acce.queuex.poll().floatValue());
mSeriesY.add(x_axis, acce.queuey.poll().floatValue());
mSeriesZ.add(x_axis++, acce.queuez.poll().floatValue());
if (x_axis % 50 == 0) {
mRenderer_x.setXAxisMin(x_axis);
mRenderer_x.setXAxisMax(x_axis + 50);
mRenderer_y.setXAxisMin(x_axis);
mRenderer_y.setXAxisMax(x_axis + 50);
mRenderer_z.setXAxisMin(x_axis);
mRenderer_z.setXAxisMax(x_axis + 50);
}
//if (mChartView_x != null) {
mChartView_x.repaint();
//}
// if (mChartView_y != null) {
mChartView_y.repaint();
//}
//if (mChartView_z != null) {
mChartView_z.repaint();
//}
}
}
}
}
我让程序运行,有时我收到错误
04-01 22:11:16.927 8936-14743/com.forcetest E/AndroidRuntime: FATAL EXCEPTION: my runnable thread
java.lang.NullPointerException
at com.forcetest.RawData$Plot.run(RawData.java:339)
at java.lang.Thread.run(Thread.java:856)
我的帖子有什么问题?
我使用ConcurrentLinkedQueue
并且它是线程安全的。
有什么问题?
由于