Darren Murtagh
Darren Murtagh

Reputation: 591

Android Cannot access accelerometer

in my android application it does not seem to let me use the accelerometer. when i call it in a statement the application crashes. the statement that seems to be causing the crash is:

SensorManager sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);

The application is simple as is only calls the accelerometer and runs when this statement is absent, the above statment is called before the onCreate method and the log cat is below

05-24 20:08:46.990: E/AndroidRuntime(1149): FATAL EXCEPTION: main
05-24 20:08:46.990: E/AndroidRuntime(1149): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.b00348312.workout/com.b00348312.workout.WorkoutAppActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
05-24 20:08:46.990: E/AndroidRuntime(1149):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at android.os.Looper.loop(Looper.java:123)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at java.lang.reflect.Method.invokeNative(Native Method) 
05-24 20:08:46.990: E/AndroidRuntime(1149):     at java.lang.reflect.Method.invoke(Method.java:521)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at dalvik.system.NativeStart.main(Native Method)
05-24 20:08:46.990: E/AndroidRuntime(1149): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
05-24 20:08:46.990: E/AndroidRuntime(1149):     at android.app.Activity.getSystemService(Activity.java:3526)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at com.b00348312.workout.WorkoutAppActivity.<init>(WorkoutAppActivity.java:25)   
05-24 20:08:46.990: E/AndroidRuntime(1149):     at java.lang.Class.newInstanceImpl(Native Method)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at java.lang.Class.newInstance(Class.java:1429)
05-24 20:08:46.990: E/AndroidRuntime(1149):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 
05-24 20:08:46.990: E/AndroidRuntime(1149):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
05-24 20:08:46.990: E/AndroidRuntime(1149):     ... 11 more

from the logcat i cannot figure out the cause of this problem when this statement worked in a different programme.

Upvotes: 1

Views: 283

Answers (2)

Broak
Broak

Reputation: 4187

Thats a double negative? lol Cut the code where ure calling the system service and paste inside your onCreate

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

//PLACE YOUR ACCEL CODE IN HERE       

    }

Upvotes: 1

DDRBoxman
DDRBoxman

Reputation: 311

You have to grab the SensorManager after onCreate is called.

"System services not available to Activities before onCreate()"

Upvotes: 0

Related Questions