SensorEventListener实现不起作用

时间:2013-04-01 17:33:37

标签: android android-sensors

我正在尝试在我的简单活动中使用SensorEventListener但是每次运行应用程序后都会使用。

这是活动代码:

package com.chovanec.elastix;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity implements SensorEventListener {

    private final SensorManager m_sensor_manager;
    private final Sensor m_accelerometer;

    public MainActivity()
    {
        m_sensor_manager = (SensorManager) getSystemService(SENSOR_SERVICE);
        m_accelerometer = m_sensor_manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    protected void onResume()
    {
        super.onResume();
        m_sensor_manager.registerListener(this, m_accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
    }

    public void onSensorChanged(SensorEvent event)
    {
        _("onSensorChanged");
        _("" + event.values[0]);
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy)
    {

    }

    private void _(String msg)
    {
        Log.d("chovanec", msg);
    }

}

这是CatLog:

04-01 19:25:16.755: D/AndroidRuntime(19142): Shutting down VM
04-01 19:25:16.755: W/dalvikvm(19142): threadid=1: thread exiting with uncaught exception (group=0x40f3e2a0)
04-01 19:25:16.765: E/AndroidRuntime(19142): FATAL EXCEPTION: main
04-01 19:25:16.765: E/AndroidRuntime(19142): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.chovanec.elastix/com.chovanec.elastix.MainActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
04-01 19:25:16.765: E/AndroidRuntime(19142):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
04-01 19:25:16.765: E/AndroidRuntime(19142):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
04-01 19:25:16.765: E/AndroidRuntime(19142):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
04-01 19:25:16.765: E/AndroidRuntime(19142):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
04-01 19:25:16.765: E/AndroidRuntime(19142):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 19:25:16.765: E/AndroidRuntime(19142):    at android.os.Looper.loop(Looper.java:137)
04-01 19:25:16.765: E/AndroidRuntime(19142):    at android.app.ActivityThread.main(ActivityThread.java:4898)
04-01 19:25:16.765: E/AndroidRuntime(19142):    at java.lang.reflect.Method.invokeNative(Native Method)
04-01 19:25:16.765: E/AndroidRuntime(19142):    at java.lang.reflect.Method.invoke(Method.java:511)
04-01 19:25:16.765: E/AndroidRuntime(19142):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
04-01 19:25:16.765: E/AndroidRuntime(19142):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
04-01 19:25:16.765: E/AndroidRuntime(19142):    at dalvik.system.NativeStart.main(Native Method)
04-01 19:25:16.765: E/AndroidRuntime(19142): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
04-01 19:25:16.765: E/AndroidRuntime(19142):    at android.app.Activity.getSystemService(Activity.java:4603)
04-01 19:25:16.765: E/AndroidRuntime(19142):    at com.chovanec.elastix.MainActivity.<init>(MainActivity.java:19)
04-01 19:25:16.765: E/AndroidRuntime(19142):    at java.lang.Class.newInstanceImpl(Native Method)
04-01 19:25:16.765: E/AndroidRuntime(19142):    at java.lang.Class.newInstance(Class.java:1319)
04-01 19:25:16.765: E/AndroidRuntime(19142):    at android.app.Instrumentation.newActivity(Instrumentation.java:1057)
04-01 19:25:16.765: E/AndroidRuntime(19142):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)
04-01 19:25:16.765: E/AndroidRuntime(19142):    ... 11 more

3 个答案:

答案 0 :(得分:1)

它清楚地说

Caused by: java.lang.IllegalStateException: 
System services not available to Activities before onCreate()

活动中没有构造函数的概念。将代码转移到活动的onCreate方法。

m_sensor_manager = (SensorManager) getSystemService(SENSOR_SERVICE);
m_accelerometer = m_sensor_manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

答案 1 :(得分:1)

删除以下代码:

public MainActivity()
{
    m_sensor_manager = (SensorManager) getSystemService(SENSOR_SERVICE);
    m_accelerometer = m_sensor_manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}

并将这些行放在onCreate()中。

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    m_sensor_manager = (SensorManager) getSystemService(SENSOR_SERVICE);
    m_accelerometer = m_sensor_manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}

答案 2 :(得分:-1)

这不正确:   m_sensor_manager =(SensorManager)getSystemService(SENSOR_SERVICE);

像这样使用:   m_sensor_manager =(SensorManager)getSystemService(Context.SENSOR_SERVICE);