【问题标题】:Stop Location Service停止定位服务
【发布时间】:2012-07-31 09:35:24
【问题描述】:

每当我尝试在 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);
};
}

【问题讨论】:

  • 07-31 10:56:36.609: E/AndroidRuntime(11337): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 07-31 10: 56:36.609: E/AndroidRuntime(11337): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 07-31 10:56:36.609: E/AndroidRuntime(11337): 在 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): at com .storetab.LocationService.onDestroy(LocationService.java:95)
  • 您在 ondestroy 中的 locationManager 为空。在调用之前进行空检查
  • 但为什么它为空?服务创建后才销毁,没有意义

标签: android android-service android-location


【解决方案1】:

在您的onStartCommand() 中,您有以下代码:

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

这将创建一个名为locationManager局部变量,它隐藏您在类顶部声明的类变量:

static LocationManager locationManager;

静态类变量 locationManager 永远不会被设置为任何东西,所以它是null in onDestroy()

要解决这个问题,只需将其更改为 onStartCommand()

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

【讨论】:

    猜你喜欢
    • 2013-09-25
    • 2019-03-03
    • 1970-01-01
    • 2014-03-04
    • 2017-05-03
    • 1970-01-01
    • 2016-08-16
    • 2020-08-09
    • 1970-01-01
    相关资源
    最近更新 更多