Michal
Michal

Reputation: 3642

SensorEventListener implementation do not work

I am trying to use SensorEventListener on my simple Activity but everytime after run app fall.

Here is activity code:

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);
    }

}

Here is 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

Upvotes: 0

Views: 5589

Answers (3)

Mehul Khot
Mehul Khot

Reputation: 1

this is not correct : m_sensor_manager = (SensorManager) getSystemService(SENSOR_SERVICE);

use like this : m_sensor_manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

Upvotes: -1

Jay Snayder
Jay Snayder

Reputation: 4338

Remove the following code:

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

and put those lines within the 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);
}

Upvotes: 1

Kumar Bibek
Kumar Bibek

Reputation: 9117

It clearly says

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

There's no concept of a constructor in an activity. Shift your code to the onCreate Method of your activity.

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

Upvotes: 1

Related Questions