编译以下代码时,出现编译错误:
Error:(154, 47) error: local variable locationListener is accessed from within inner class; needs to be declared final
在locationListener
中的以下一行:
getDistance(locationA,locationListener );
lm.removeUpdates(locationListener);
如何阻止GPS更新?
locationListener = new LocationListener() {
@Override
public void onLocationChanged(final Location location) {
Location net_loc, gps_loc;
net_loc = location;
while (net_loc != null) {
showCurrentLocation(net_loc);
Location locationA = new Location("A");
locationA.setLatitude(location.getLatitude());
locationA.setLongitude(location.getLongitude());
getDistance(locationA,locationListener );
lm.removeUpdates(locationListener);
}
答案 0 :(得分:0)
尝试
lm.removeUpdates(this);
而不是
lm.removeUpdates(locationListener);
还有一点要记住,一旦在代码中输入while循环,它就会持续运行直到net_loc==null
,所以lm.removeUpdates(locationListener)
每次都会调用。