title: Issue with using Accelerometer and GPS at the same time in Android Studio
body: <p>I'm pretty new to programming in android studio, and I've been trying to figure this out for a while, but I'm not sure what the problem is. I'm trying to use a combination of the Accelerometer and GPS at the same time to get a more accurate number for the acceleration and speed of the device, but when I try to start the accelerometer it throws this exception:</p>
  
  <pre><code>java.lang.RuntimeException: Unable to start activity ComponentInfo
  </code></pre>
  
  <p>Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()</p>
  
  <p>Here is the MainActivity:</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.example.accelerometergps;
  
  import android.content.Context;
  import android.location.Location;
  import android.location.LocationListener;
  import android.location.LocationManager;
  import android.support.v7.app.ActionBarActivity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.widget.TextView;
  
  public class MainActivity extends ActionBarActivity implements LocationListener {
  
      private Accelerometer a;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          LocationManager l = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
          l.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
          this.onLocationChanged(null);
          a = new Accelerometer();
          //It works fine if I remove the following line
          a.registerListener();
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      @Override
      public void onLocationChanged(Location location) {
          TextView tv = (TextView) findViewById(R.id.textView);
          if (location == null) {
              tv.setText("no GPS");
          } else {
              double temp = location.getSpeed();
              double speed = 2.23694 * temp;
              //            tv.setText(String.valueOf(speed));
          }
      }
  
      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {
  
      }
  
      @Override
      public void onProviderEnabled(String provider) {
  
      }
  
      @Override
      public void onProviderDisabled(String provider) {
  
      }
  }
  </code></pre>
  
  <p>Here is the Accelerometer Class:</p>
  
  
  
  <pre><code>package com.example.accelerometergps;
  
  
  import android.app.Activity;
  import android.content.Context;
  import android.hardware.Sensor;
  import android.hardware.SensorEvent;
  import android.hardware.SensorEventListener;
  import android.hardware.SensorManager;
  import android.os.Bundle;
  import android.widget.EditText;
  import android.widget.TextView;
  
  public class Accelerometer extends Activity implements SensorEventListener {
      private SensorManager mSensorManager;
  
      private Sensor mAccelerometer;
      double[] accelerationList = new double[5];
      double averageAcceleration;
  
  
      protected void onResume() {
          super.onResume();
          mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
      }
      protected void onPause() {
          super.onPause();
          mSensorManager.unregisterListener(this);
      }
  
      @Override
      public void onAccuracyChanged(Sensor sensor, int accuracy) {
  
      }
  
      public void registerListener() {
          mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
          mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
          mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
      }
  
      public void unregisterListener() {
          mSensorManager.unregisterListener(this);
      }
  
      @Override
      public void onSensorChanged(SensorEvent event) {
  
          float x = event.values[0];
          float y = event.values[1];
          float z = event.values[2];
  
          double temp = sumAcceleration(x, y, z);
  
          addToList(temp);
          averageAcceleration = accelerationListAverage();
  
      }
  
      public double sumAcceleration(float i, float j, float k) {
          float temp = i * i + j * j + k * k;
  
          double linearAcceleration = Math.sqrt(temp) - 9.8;
          return linearAcceleration;
  
      }
  
      public double accelerationListAverage() {
          double sum = 0;
          for (int i = 0; i &lt; 5; i++) {
              sum += accelerationList[i];
          }
  
          return (sum / 5);
      }
  
      public void addToList(double a) {
          double b = accelerationList[0];
          double c = accelerationList[1];
          double d = accelerationList[2];
          double e = accelerationList[3];
          accelerationList[0] = a;
          accelerationList[1] = b;
          accelerationList[2] = c;
          accelerationList[3] = d;
          accelerationList[4] = e;
      }
  
      public double getAcceleration() {
          return averageAcceleration;
      }
  
  }
  </code></pre>
  
  <p>Any help or suggestions would be greatly appreciated, thank you in advance.</p>