0
每当我尝试停止在android上的位置服务我得到一个NullPointerException。任何人都有一些关于如何做到这一点的提示?我想实现一些onstop()和ondestroy()方法的活动。这里是我的服务代码:停止位置服务
LocationService.Java
包com.storetab;
public class LocationService extends Service {
static LocationManager locationManager;
static Location lastknown;
final static String MY_ACTION = "MY_ACTION";
static LocationListener ll;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
final Criteria criteria = new Criteria();
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
criteria.setCostAllowed(true);
locationManager.getBestProvider(criteria, true);
ll = new MyLocListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);
lastknown = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Log.d("Teste","lastknown");
Intent intent1 = new Intent();
intent.putExtra("location1", lastknown);
intent.setAction(MY_ACTION);
sendBroadcast(intent1);
Log.d("broadcastlast","lastknown");
return START_STICKY;
}
private class MyLocListener implements LocationListener {
public void onLocationChanged(Location location) {
}
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
Log.d("1Provider DIsabled", "Provider Disabled");
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Log.d("1Provider Enabled", "Provider Enabled");
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
Log.d("1Provider Changed", "Service Status Changed");
}
}
@Override public void onDestroy() {
locationManager.removeUpdates(ll);
};
}
交logcat的输出 – nandeesh 2012-07-31 09:37:14
10月7日至31日:56:36.609:E/AndroidRuntime(11337):\t在com.android.internal。 os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 07-31 10:56:36.609:E/AndroidRuntime(11337):\t at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551 ) 07-31 10:56:36.609:E/AndroidRuntime(11337):\t at dalvik.system.NativeStart.main(Native Method) 07-31 10:56:36.609:E/AndroidRuntime(11337):由:java。 lang.NullPointerException 07-31 10:56:36.609:E/AndroidRuntime(11337):\t at com.storetab.LocationService.onDestroy(LocationService.java:95) – user1437481 2012-07-31 09:58:29
ondestroy中的locationManager为null。在电话 – nandeesh 2012-07-31 10:23:02