views:

296

answers:

1

I am having issues regarding a LocationListener in my Service called myService.

Here is my code:

///onStart method ..
onStart() {
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE)
    .
    .
    provider = locationManager.getBestProvider(criteria, true);
    locationListener = (LocationListener) new MyLocationListener();
    locationManager.requestLocationUpdates(provider, 0, 0, locationListener);
}

In my Activity there is a button which should stop the service. On the click of the button I am doing:

stopService(new Intent(getApplicationContext(), myService.class)

In the myService.java file I have:

////onDestroy method....
onDestroy() {
    locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.removeUpdates(locationListener); //Getting **exception here ....
}

I get the following Exception:

java.lang.IllegalArgumentException: listener==null at android.location.LocationManager.removeUpdates(LocationManager.java:799) at 
<.package_name>myService.onDestroy(myService.java:407) 

I don't know why listener turns null without reason. Please can you tell me where I am going wrong!

A: 

I could be off here, but it seems like maybe your Service is being killed then (later) restarted. Why not move the instantiation stuff into onCreate()?

fiXedd
thanks for the ans. can u please brief what may be the reasons/causes for the service to restart.
sheik