使用Android的sensorManager时出错

时间:2016-04-01 09:09:01

标签: android android-sensors

我有一个使用sensorManager的类,并且总是在

行失败
sensorManager = (SensorManager) getSystemService(Activity.SENSOR_SERVICE);

我的全班是

public class AcceSensorInterface extends AppCompatActivity implements SensorEventListener {

    private SensorManager sensorManager;
    private Sensor accelerometer;

    public Queue<Float> queuex;
    public Queue<Float> queuey;
    public Queue<Float> queuez;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public void createSensorManager(){
        sensorManager = (SensorManager) getSystemService(Activity.SENSOR_SERVICE);
        if (sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null) {
            // success! we have an accelerometer
            accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
            // vibrateThreshold = accelerometer.getMaximumRange() / 2;
        } else {
            // fai! we dont have an accelerometer!
        }

        queuex = new ConcurrentLinkedQueue<Float>();
        queuey = new ConcurrentLinkedQueue<Float>();
        queuez = new ConcurrentLinkedQueue<Float>();
    }

    @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) {

    }

    public void deleteQueues(){
        sensorManager.unregisterListener(this);
        queuex.clear();
        queuey.clear();
        queuez.clear();
    }
}

可能出现什么问题?

编辑: 错误日志是

04-01 18:03:52.025 31752-31752/com.abbott.forcetest E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      java.lang.RuntimeException: Unable to resume activity {com.forcetest/com.forcetest.MainActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
                                                                          at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2616)
                                                                          at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2644)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                          at android.os.Looper.loop(Looper.java:137)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:4898)
                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:511)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
                                                                          at dalvik.system.NativeStart.main(Native Method)
                                                                       Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
                                                                          at android.app.Activity.getSystemService(Activity.java:4603)
                                                                          at com.abbott.forcetest.AcceInterface.createSensor(AcceInterface.java:25)
                                                                          at com.abbott.forcetest.RawData.begin_process(RawData.java:277)
                                                                          at com.abbott.forcetest.RawData.onResume(RawData.java:211)
                                                                          at android.support.v4.app.Fragment.performResume(Fragment.java:2020)
                                                                          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1107)
                                                                          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
                                                                          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1234)
                                                                          at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:2056)
                                                                          at android.support.v4.app.FragmentController.dispatchResume(FragmentController.java:196)
                                                                          at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:505)
                                                                          at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:494)
                                                                          at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:161)
                                                                          at android.app.Activity.performResume(Activity.java:5293)
                                                                          at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2606)
                                                                          at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2644) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                          at android.os.Looper.loop(Looper.java:137) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:4898) 
                                                                          at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                          at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775) 
                                                                          at dalvik.system.NativeStart.main(Native Method) 

由于

2 个答案:

答案 0 :(得分:0)

你需要在super.onCreate(savedInstanceState)之后使用createSensorManager()方法;活动方法。

答案 1 :(得分:0)

我改为Java类,在构造函数中,我传递了Context。它现在有效。我的代码如下。

public class AcceInterface implements SensorEventListener {
    private SensorManager sensorManager;
    private Sensor accelerometer;
    public Queue<Float> queuex;
    public Queue<Float> queuey;
    public Queue<Float> queuez;
    public AcceInterface(Context context){

        sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        if (sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null) {
            // success! we have an accelerometer
            accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
            // vibrateThreshold = accelerometer.getMaximumRange() / 2;
        } else {
            // fai! we dont have an accelerometer!
        }

        queuex = new ConcurrentLinkedQueue<Float>();
        queuey = new ConcurrentLinkedQueue<Float>();
        queuez = new ConcurrentLinkedQueue<Float>();
    }

    public void deleteSensor(){
        sensorManager.unregisterListener(this);
        queuex.clear();
        queuey.clear();
        queuez.clear();
    }
    @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) {

    }
}