Android 为什么我的LocationListener变为空?

Android 为什么我的LocationListener变为空?,android,location,locationmanager,locationlistener,Android,Location,Locationmanager,Locationlistener,我在我的名为myService的服务中遇到有关LocationListener的问题 这是我的密码: ///onStart method .. onStart() { locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE) . . provider = locationManager.getBestProvider(criteria, true); loc

我在我的名为
myService
的服务中遇到有关
LocationListener
的问题

这是我的密码:

///onStart method ..
onStart() {
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE)
    .
    .
    provider = locationManager.getBestProvider(criteria, true);
    locationListener = (LocationListener) new MyLocationListener();
    locationManager.requestLocationUpdates(provider, 0, 0, locationListener);
}
在我的活动中,有一个按钮可以停止服务。单击我正在执行的按钮时:

stopService(new Intent(getApplicationContext(), myService.class)
myService.java
文件中,我有:

////onDestroy method....
onDestroy() {
    locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.removeUpdates(locationListener); //Getting **exception here ....
}
我得到以下例外情况:

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

我不知道为什么监听器会无缘无故地变为空。请你告诉我哪里出了问题

我可能会离开这里,但看起来您的服务可能会被终止,然后(稍后)重新启动。为什么不将实例化内容移动到
onCreate()

只有在内存不足时,Android系统才会强制停止服务,并且它必须为以用户为中心的活动恢复系统资源。如果服务绑定到具有用户焦点的活动,那么它被终止的可能性较小,如果服务被声明为在前台运行(稍后讨论),那么它几乎永远不会被终止。否则,如果服务已启动且长期运行,则系统将随着时间的推移降低其在后台任务列表中的位置,并且如果您的服务已启动,则服务将极易被终止,那么您必须将其设计为优雅地处理系统重新启动。如果系统终止您的服务,它会在资源再次可用时重新启动服务(尽管这也取决于您从onStartCommand()返回的值,稍后将讨论)。有关系统何时可能销毁服务的更多信息,请参阅进程和线程文档。

是否在代码中的其他地方手动将其设置为null?我确信我没有将其设置为null。我认为服务重新启动了,但我不明白当服务重新启动时,它会执行哪部分代码。感谢ans。请简要说明服务重新启动的原因。