我正在编写一个应用程序,用于在X,Y和Z方向上显示Android中加速度计的值。我是一个Android noob,所以请耐心等待。这是我的代码,它很简单,但我收到一个空指针异常,我不知道我的代码有什么问题。谢谢!
package com.*****.accelo;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
public class AcceloActivity extends Activity implements SensorEventListener{
TextView textViewX, textViewY, textViewZ, textViewError;
StringBuilder builderx = new StringBuilder();
StringBuilder buildery = new StringBuilder();
StringBuilder builderz = new StringBuilder();
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
textViewError = (TextView) findViewById(R.id.textViewError);
textViewX = (TextView) findViewById(R.id.textViewX);
textViewY = (TextView) findViewById(R.id.textViewY);
textViewX = (TextView) findViewById(R.id.textViewZ);
SensorManager manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor accelerometer = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if(!manager.registerListener(this, accelerometer,
SensorManager.SENSOR_DELAY_UI)){
setContentView(textViewError);
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
builderx.setLength(0);
buildery.setLength(0);
builderz.setLength(0);
builderx.append("X " + event.values[0]);
buildery.append("Y"+ event.values[1] );
builderz.append("Z " + event.values[2]);
textViewX.setText(builderx.toString());
textViewY.setText(buildery.toString());
textViewZ.setText(builderz.toString());
}
}
来自logcat:
10-24 19:52:43.574: ERROR/AndroidRuntime(711): java.lang.RuntimeException: Unable to
start activity ComponentInfo{com.amrit.accelo/com.amrit.accelo.AcceloActivity}:
java.lang.NullPointerException
10-24 19:52:43.574: ERROR/AndroidRuntime(711): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-24 19:52:43.574: ERROR/AndroidRuntime(711): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-24 19:52:43.574: ERROR/AndroidRuntime(711): at
android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-24 19:52:43.574: ERROR/AndroidRuntime(711): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-24 19:52:43.574: ERROR/AndroidRuntime(711): at
android.os.Handler.dispatchMessage(Handler.java:99)
10-24 19:52:43.574: ERROR/AndroidRuntime(711): at
android.os.Looper.loop(Looper.java:123)
10-24 19:52:43.574: ERROR/AndroidRuntime(711): at
android.app.ActivityThread.main(ActivityThread.java:3683)
10-24 19:52:43.574: ERROR/AndroidRuntime(711): at
java.lang.reflect.Method.invokeNative(Native Method)
10-24 19:52:43.574: ERROR/AndroidRuntime(711): at
java.lang.reflect.Method.invoke(Method.java:507)
10-24 19:52:43.574: ERROR/AndroidRuntime(711): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-24 19:52:43.574: ERROR/AndroidRuntime(711): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-24 19:52:43.574: ERROR/AndroidRuntime(711): at
dalvik.system.NativeStart.main(Native Method)
10-24 19:52:43.574: ERROR/AndroidRuntime(711): Caused by: java.lang.NullPointerException
10-24 19:52:43.574: ERROR/AndroidRuntime(711): at
com.amrit.accelo.AcceloActivity.onCreate(AcceloActivity.java:50)
10-24 19:52:43.574: ERROR/AndroidRuntime(711): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-24 19:52:43.574: ERROR/AndroidRuntime(711): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
谢谢!
答案 0 :(得分:3)
您从第50行收到此错误:
textViewError.setText("Error, could not register sensor listener");
所以你的textViewError
变量在这里为空。您需要创建XML布局,使用setContentView
,然后将TextView添加到XML布局,然后使用findViewById
将TextView分配给它。 This is probably a good place to start
编辑:
为了清楚起见,您需要在调用findViewById之前设置内容视图。
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
textViewError = (TextView) findViewById(R.id.textViewError);
textViewX = (TextView) findViewById(R.id.textViewX);
textViewY = (TextView) findViewById(R.id.textViewY);
textViewX = (TextView) findViewById(R.id.textViewZ);
...
重要的是,您必须在yourlayout.xml
下有一个名为res/layout/
的xml布局文件,其中包含您要加载的视图。