我在我的服務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