title: Android app crashing when starting service that uses LocationListener, any ideas?
body: <p>I'm struggling to understand why this application is crashing, when I add the request for location updates.</p>
  
  <p>I have in my activity:</p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_my);
          startService(new Intent(this, LocationService.class));
      }
  </code></pre>
  
  <p>And the code for my service is as follows:</p>
  
  <pre><code>import android.app.Service;
  import android.content.Context;
  import android.content.Intent;
  import android.location.Location;
  import android.location.LocationListener;
  import android.location.LocationManager;
  import android.os.Bundle;
  import android.os.IBinder;
  import android.widget.Toast;
  
  public class LocationService extends Service {
      public LocationManager locationManager;
      public MyLocationListener listener;
  
      @Override
      public void onCreate() {
          super.onCreate();
      }
  
      @Override
      public void onStart(Intent intent, int startId) {
          locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
          listener = new MyLocationListener();
          locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 4000, 0, listener);
          locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 4000, 0, listener);
      }
  
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
      @Override
      public void onDestroy() {
          super.onDestroy();
          locationManager.removeUpdates(listener);
      }
  
      public class MyLocationListener implements LocationListener {
  
          public void onLocationChanged(final Location loc) {
                  Toast.makeText(getApplicationContext(), "New Location - Lat: " + String.valueOf(loc.getLatitude())
                   + "; Lon: " + String.valueOf(loc.getLongitude()), Toast.LENGTH_SHORT).show();
          }
  
          public void onProviderDisabled(String provider) {
              Toast.makeText(getApplicationContext(), "GPS Disabled", Toast.LENGTH_SHORT).show();
          }
  
          public void onProviderEnabled(String provider) {
              Toast.makeText(getApplicationContext(), "GPS Enabled", Toast.LENGTH_SHORT).show();
          }
  
          public void onStatusChanged(String provider, int status, Bundle extras) {
  
          }
  
      }
  }
  </code></pre>
  
  <p>At the minute, I just want to get a toast working. Later I plan to return the data to the activity via a broadcast, but can't get my head around why this toast isn't working.</p>
  
  <p>If I comment out the code in onStart and replace it with a toast then the toast is displayed. </p>
  
  <p>Does anyone have an idea as to why it isn't working? </p>
  
  <p>I've registered the service in <em>AndroidManifest.xml</em></p>
  
  <pre><code>    &lt;service
          android:name=".LocationService"&gt;
      &lt;/service&gt;
  
      &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;
      &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /&gt;
  </code></pre>
  
  <p>Any guidance will be appreciated.</p>
  
  <p>Thanks</p>