我是Android应用程序开发的新手,仍然通过其他人的例子学习。我从this页面获取此代码,并且我一直在尝试使用此代码查找问题。基本上,我试图将加速度计数据绘制到图表中。当我运行程序时,一切似乎都很好。但是当我按下停止按钮时,它会崩溃。希望有人可以帮助我。提前致谢!
这是代码。
public class MainActivity extends Activity implements SensorEventListener,
OnClickListener {
private SensorManager sensorManager;
private Button btnStart, btnStop, btnUpload;
private boolean started;
private ArrayList<AccelData> sensorData;
private LinearLayout layout;
private View mChart;
@Override
public 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);
sensorData = new ArrayList();
btnStart = (Button) findViewById(R.id.btnStart);
btnStop = (Button) findViewById(R.id.btnStop);
btnUpload = (Button) findViewById(R.id.btnUpload);
btnStart.setOnClickListener(this);
btnStop.setOnClickListener(this);
btnUpload.setOnClickListener(this);
btnStart.setEnabled(true);
btnStop.setEnabled(false);
if (sensorData == null || sensorData.size() == 0) {
btnUpload.setEnabled(false);
}
}
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
if (started == true) {
sensorManager.unregisterListener(this);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
if (started) {
double x = event.values[0];
double y = event.values[1];
double z = event.values[2];
long timestamp = System.currentTimeMillis();
AccelData data = new AccelData(timestamp, x, y, z);
sensorData.add(data);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStart:
btnStart.setEnabled(false);
btnStop.setEnabled(true);
btnUpload.setEnabled(false);
sensorData = new ArrayList();
// save previous data if available
started = true;
Sensor accel = sensorManager
.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(this, accel,
SensorManager.SENSOR_DELAY_FASTEST);
break;
case R.id.btnStop:
btnStart.setEnabled(true);
btnStop.setEnabled(false);
btnUpload.setEnabled(true);
started = false;
sensorManager.unregisterListener(this);
layout.removeAllViews();
openChart();
// show data in chart
break;
case R.id.btnUpload:
break;
default:
break;
}
}
private void openChart() {
if (sensorData != null || sensorData.size() > 0) {
long t = sensorData.get(0).getTimestamp();
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
XYSeries xSeries = new XYSeries("X");
XYSeries ySeries = new XYSeries("Y");
XYSeries zSeries = new XYSeries("Z");
for (AccelData data : sensorData) {
xSeries.add(data.getTimestamp() - t, data.getX());
ySeries.add(data.getTimestamp() - t, data.getY());
zSeries.add(data.getTimestamp() - t, data.getZ());
}
dataset.addSeries(xSeries);
dataset.addSeries(ySeries);
dataset.addSeries(zSeries);
XYSeriesRenderer xRenderer = new XYSeriesRenderer();
xRenderer.setColor(Color.RED);
xRenderer.setPointStyle(PointStyle.CIRCLE);
xRenderer.setFillPoints(true);
xRenderer.setLineWidth(1);
xRenderer.setDisplayChartValues(false);
XYSeriesRenderer yRenderer = new XYSeriesRenderer();
yRenderer.setColor(Color.GREEN);
yRenderer.setPointStyle(PointStyle.CIRCLE);
yRenderer.setFillPoints(true);
yRenderer.setLineWidth(1);
yRenderer.setDisplayChartValues(false);
XYSeriesRenderer zRenderer = new XYSeriesRenderer();
zRenderer.setColor(Color.BLUE);
zRenderer.setPointStyle(PointStyle.CIRCLE);
zRenderer.setFillPoints(true);
zRenderer.setLineWidth(1);
zRenderer.setDisplayChartValues(false);
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
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);
for (int i = 0; i < sensorData.size(); i++) {
multiRenderer.addXTextLabel(i + 1, ""
+ (sensorData.get(i).getTimestamp() - t));
}
for (int i = 0; i < 12; i++) {
multiRenderer.addYTextLabel(i + 1, "" + i);
}
multiRenderer.addSeriesRenderer(xRenderer);
multiRenderer.addSeriesRenderer(yRenderer);
multiRenderer.addSeriesRenderer(zRenderer);
// Getting a reference to LinearLayout of the MainActivity Layout
layout = (LinearLayout) findViewById(R.id.chart_container);
// Creating a Line Chart
mChart = ChartFactory.getLineChartView(getBaseContext(), dataset,
multiRenderer);
// Adding the Line Chart to the LinearLayout
layout.addView(mChart);
}
}
}
虽然这是加速度计类的代码。
public class AccelData {
private long timestamp;
private double x;
private double y;
private double z;
public AccelData(long timestamp, double x, double y, double z) {
this.timestamp = timestamp;
this.x = x;
this.y = y;
this.z = z;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public double getZ() {
return z;
}
public void setZ(double z) {
this.z = z;
}
public String toString()
{
return "t="+timestamp+", x="+x+", y="+y+", z="+z;
}
}
这是我的xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="25dp" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="60" >
<Button
android:id="@+id/btnStart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="Start"
android:textSize="20sp" />
<Button
android:id="@+id/btnStop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="Stop"
android:textSize="20sp" />
<Button
android:id="@+id/btnUpload"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="Upload"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/chart_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/btnUpload" >
</LinearLayout>
</LinearLayout>
这是我的LogCat
02-08 00:23:49.786: E/SELinux(31829): Function: selinux_android_load_priority [0], There is no sepolicy file
02-08 00:23:49.786: E/SELinux(31829):
02-08 00:23:49.786: E/SELinux(31829): Function: selinux_android_load_priority , loading version is VE=SEPF_GT-I9505_4.3_0018
02-08 00:23:49.786: E/SELinux(31829):
02-08 00:23:49.786: E/SELinux(31829):
02-08 00:23:49.786: E/SELinux(31829): selinux_android_seapp_context_reload: seapp_contexts file is loaded from /data/security/spota/seapp_contexts
02-08 00:23:49.786: D/dalvikvm(31829): Late-enabling CheckJNI
02-08 00:23:50.116: D/libEGL(31829): loaded /vendor/lib/egl/libEGL_adreno.so
02-08 00:23:50.126: D/libEGL(31829): loaded /vendor/lib/egl/libGLESv1_CM_adreno.so
02-08 00:23:50.146: D/libEGL(31829): loaded /vendor/lib/egl/libGLESv2_adreno.so
02-08 00:23:50.146: I/Adreno-EGL(31829): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build: (CL4169980)
02-08 00:23:50.146: I/Adreno-EGL(31829): OpenGL ES Shader Compiler Version: 17.01.10.SPL
02-08 00:23:50.146: I/Adreno-EGL(31829): Build Date: 09/26/13 Thu
02-08 00:23:50.146: I/Adreno-EGL(31829): Local Branch:
02-08 00:23:50.146: I/Adreno-EGL(31829): Remote Branch:
02-08 00:23:50.146: I/Adreno-EGL(31829): Local Patches:
02-08 00:23:50.146: I/Adreno-EGL(31829): Reconstruct Branch:
02-08 00:23:50.306: D/OpenGLRenderer(31829): Enabling debug mode 0
02-08 00:23:50.396: V/RenderScript(31829): 0x75ff4a10 Launching thread(s), CPUs 4
02-08 00:23:53.509: D/SensorManager(31829): registerListener :: create queue :: handler = 0, name = K330 3-axis Accelerometer, delay = 0,
02-08 00:24:14.530: D/SensorManager(31829): unregisterListener ::
02-08 00:24:15.171: D/AndroidRuntime(31829): Shutting down VM
02-08 00:24:15.171: W/dalvikvm(31829): threadid=1: thread exiting with uncaught exception (group=0x41928898)
02-08 00:24:15.181: E/AndroidRuntime(31829): FATAL EXCEPTION: main
02-08 00:24:15.181: E/AndroidRuntime(31829): java.lang.NullPointerException
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:739)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.GLES20RecordingCanvas.drawBitmap(GLES20RecordingCanvas.java:91)
02-08 00:24:15.181: E/AndroidRuntime(31829): at org.achartengine.GraphicalView.onDraw(GraphicalView.java:174)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.View.draw(View.java:14853)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.View.getDisplayList(View.java:13744)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.View.getDisplayList(View.java:13786)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.View.draw(View.java:14563)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.ViewGroup.drawChild(ViewGroup.java:3316)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3153)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.View.getDisplayList(View.java:13739)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.View.getDisplayList(View.java:13786)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.View.draw(View.java:14563)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.ViewGroup.drawChild(ViewGroup.java:3316)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3153)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.View.getDisplayList(View.java:13739)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.View.getDisplayList(View.java:13786)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.View.draw(View.java:14563)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.ViewGroup.drawChild(ViewGroup.java:3316)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3153)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.View.getDisplayList(View.java:13739)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.View.getDisplayList(View.java:13786)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.View.draw(View.java:14563)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.ViewGroup.drawChild(ViewGroup.java:3316)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3153)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.View.getDisplayList(View.java:13739)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.View.getDisplayList(View.java:13786)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.View.draw(View.java:14563)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.ViewGroup.drawChild(ViewGroup.java:3316)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3153)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.View.draw(View.java:14856)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.widget.FrameLayout.draw(FrameLayout.java:467)
02-08 00:24:15.181: E/AndroidRuntime(31829): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2621)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.View.getDisplayList(View.java:13744)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.View.getDisplayList(View.java:13786)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1411)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1359)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.ViewRootImpl.draw(ViewRootImpl.java:2670)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2536)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2152)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1247)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6355)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.Choreographer.doCallbacks(Choreographer.java:591)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.Choreographer.doFrame(Choreographer.java:561)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.os.Handler.handleCallback(Handler.java:730)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.os.Handler.dispatchMessage(Handler.java:92)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.os.Looper.loop(Looper.java:137)
02-08 00:24:15.181: E/AndroidRuntime(31829): at android.app.ActivityThread.main(ActivityThread.java:5419)
02-08 00:24:15.181: E/AndroidRuntime(31829): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 00:24:15.181: E/AndroidRuntime(31829): at java.lang.reflect.Method.invoke(Method.java:525)
02-08 00:24:15.181: E/AndroidRuntime(31829): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
02-08 00:24:15.181: E/AndroidRuntime(31829): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
02-08 00:24:15.181: E/AndroidRuntime(31829): at dalvik.system.NativeStart.main(Native Method)
02-08 00:24:17.192: I/Process(31829): Sending signal. PID: 31829 SIG: 9