我在我的服务myService
中遇到有关LocationListener
的问题。为什么我的LocationListener变为null?
这里是我的代码:
///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)
我不知道为什么监听器无理由变为空。请你能告诉我我哪里出错了!
你是否手动将它设置为null在代码中的其他位置? – JRL 2010-01-28 11:01:47
我确定不会将它设置为空。我认为服务重新启动,但我不明白当服务重新启动时它会执行哪部分代码。 – sheik 2010-02-01 05:48:25