我想在服务中注册一个SENSOR_ACCELEROMETER
,但事实证明这是一个无关紧要的问题。以下是我的代码:
asensor = new AccerSensor(context);//the sensoreventlistener
Log.i("wogua", "creating"+asensor.toString());
manager.registerListener(asensor,manager.getDefaultSensor(SensorManager.SENSOR_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST);
Exception:
Caused by: java.lang.NullPointerException::
at com.wogua.mobileprotected.service.LostProtectService.onCreate(LostProtectService.java:36)
我不知道问题是什么,sensoreventlistener的实例不是null :::: com.wogua.mobileprotected.sensor.AccerSensor@41bf3f20
怎么可能是null,::: manager =(SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
答案 0 :(得分:1)
这是适合我的方式:
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
if (mAccelerometer != null) {
// Success! There's an accelerometer
mSensorManager.registerListener(this, mAccelerometer,
SensorManager.SENSOR_DELAY_FASTEST);
} else {
Toast.makeText(this, "This device doesnt support accelerometer",
Toast.LENGTH_SHORT).show();
stopSelf();
}
答案 1 :(得分:0)
manager
未初始化,超过确定