Why is my LocationListener getting null?

I am having problems with LocationListenerin 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 that should stop the service. I click the button that I am doing:

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

In the file myService.javaI 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 do not know why the listener goes to zero for no reason. Please tell me where I am going wrong!

+3
source share
2 answers

Android , , , . , , , ​​ ( ), . , , , - , . , , ( , onStartCommand(), ). , , . Threading.

+1

, , , () . onCreate()?

0

Source: https://habr.com/ru/post/1730359/


All Articles