NullpointerException when calling getSystemService(Context.LOCATION_SERVICE) and onLocationChanged not called

Siddharth picture Siddharth · Jan 22, 2013 · Viewed 8.8k times · Source

I am trying to create a background service that updates the current gps position. I am getting a NullPointerException at line LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

HomeActivity launches a service

startService(new Intent(getApplicationContext(), ForHire.class));

Service (ForHire) creates a TimerTask Updates

public class ForHire extends Service {

...
private Timer getUpdatesNow = new Timer();
private Updates updates = new Updates(getUpdatesNow);

@Override
public void onCreate() {
    ...
    getUpdatesNow.schedule(updates, God.KM20TIME);
    Log.v("Taxeeta", "Notification created");
}

    private class Updates extends TimerTask implements LocationListener {
    private Timer getUpdatesNow;

    public Updates(Timer newGetUpdatesNow) {
        super();
        getUpdatesNow = newGetUpdatesNow;
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                God.KM20TIME, God.KM20DISTANCE, (LocationListener) this);
    }
    public void run() {
        ...
        //do some cleanup
    }
            @Override
    public void onLocationChanged(Location location) {
        Log.v("Taxeeta", "Location changed");
        // do a update of the current location.
    }

The first issue is that I get that NullPointerException. Second issue is that onLocationChanged is never called if I comment out LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, God.KM20TIME, God.KM20DISTANCE, (LocationListener) this); these 2 lines.

My Manifest

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application ...>
   ...
   <uses-library android:name="com.google.android.maps" />
</application>

What am I missing here ?

Edit : Values of KM20TIME = 5000 (5seconds), and KM20DISTANCE = 1 (1meter). Apart from the fix's below, I walked out of my house, got GPS enabled and walked from one corner of my balcony to the other. I noticed that my gps (LSB) changed as I walked from one corner to the other every 5 seconds.

Answer

Sam picture Sam · Jan 22, 2013

The earliest you can call new Updates(getUpdatesNow); is inside your Service's onCreate() method. The simple reason is that getSystemService() requires a valid Context which doesn't exist before entering onCreate().

Try:

private Timer getUpdatesNow;
private Updates updates;

@Override
public void onCreate() {
    ...
    getUpdatesNow = new Timer();
    updates = new Updates(getUpdatesNow);