0

我正在尝试在我的简单 Activity 上使用 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);
    }

}

这是猫日志:

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
4

3 回答 3

1

它清楚地说

引起:java.lang.IllegalStateException:
系统服务在 onCreate() 之前对活动不可用

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

m_sensor_manager = (SensorManager) getSystemService(SENSOR_SERVICE);
m_accelerometer = m_sensor_manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
于 2013-04-01T17:36:10.977 回答
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);
}
于 2013-04-01T17:44:46.007 回答
-1

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

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

于 2015-02-14T07:25:03.850 回答