我想建立一个能够记录此链接加速计数据的应用程序: http://mobile.tutsplus.com/tutorials/android/android-barometer-logger-acquiring-sensor-data/
事情就是我收到了一些错误而且不明白为什么我会收到这些错误。 我在下面粘贴我的代码:
import java.text.SimpleDateFormat;
import java.util.Date;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity implements SensorEventListener {
SensorManager sensorManager = null;
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
SensorEventListener myListenerInstance = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
setContentView(R.layout.activity_main);
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
sensorManager.registerListener(myListenerInstance, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
long timestamp = event.timestamp;
float value = event.values[0];
// String res=String.valueOf(event.values[0])+"#"+String.valueOf(event.values[1])+"#"+String.valueOf(event.values[2]);
// Log.d("test", res);
}
protected void onResume(){
super.onResume();
sensorManager.registerListener(myListenerInstance, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onStop(){
sensorManager.unregisterListener(myListenerInstance);
super.onStop();
}
}
我得到的错误是:
06-22 21:44:27.322: E/AndroidRuntime(9918): FATAL EXCEPTION: main
06-22 21:44:27.322: E/AndroidRuntime(9918): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.rasam.sampleproject/com.rasam.sampleproject.MainActivity}: java.lang.NullPointerException
06-22 21:44:27.322: E/AndroidRuntime(9918): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): at android.app.ActivityThread.access$600(ActivityThread.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): at android.os.Handler.dispatchMessage(Handler.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): at android.os.Looper.loop(Looper.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): at android.app.ActivityThread.main(ActivityThread.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): at java.lang.reflect.Method.invokeNative(Native Method)
06-22 21:44:27.322: E/AndroidRuntime(9918): at java.lang.reflect.Method.invoke(Method.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): at dalvik.system.NativeStart.main(Native Method)
06-22 21:44:27.322: E/AndroidRuntime(9918): Caused by: java.lang.NullPointerException
06-22 21:44:27.322: E/AndroidRuntime(9918): at com.rasam.sampleproject.MainActivity.<init>(MainActivity.java:26)
06-22 21:44:27.322: E/AndroidRuntime(9918): at java.lang.Class.newInstanceImpl(Native Method)
06-22 21:44:27.322: E/AndroidRuntime(9918): at java.lang.Class.newInstance(Class.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): at android.app.Instrumentation.newActivity(Instrumentation.java)
06-22 21:44:27.322: E/AndroidRuntime(9918): ... 12 more
有人能告诉我为什么会这样吗???
答案 0 :(得分:0)
SensorManager sensorManager = null;
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
在将sensorManager初始化为null之后,您立即尝试调用其中一个方法,这只能导致空指针异常。
在尝试使用sensorManager之前,必须确保始终执行初始化sensorManager的后续代码行:
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
可能你应该在类的顶部将sensor初始化为null,然后让自己成为一个真正的sensorManager,然后只在你的onCreate()方法中使用真正的传感器。
SensorManager sensorManager = null;
Sensor sensor = null;
SensorEventListener myListenerInstance = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
setContentView(R.layout.activity_main);
}