2015-11-14 90 views
-1

当编译下面的代码,我得到的编译错误:如何防止GPS更新?

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

要么声明'locationListener'' final',要么将其声明移至类范围。 –

+0

我这样做,但它永远不会停止GPS更新 –

+0

[你之前没有搜索问](http://stackoverflow.com/search?q= [爪哇] +%22is +访问+从+内+内+类%3B + need + to + be +%+%22 + is%3Aquestions + answers%3A1)28个答案至今为止。 –

回答

0

lm.removeUpdates(this); 

,而不是

lm.removeUpdates(locationListener); 

也有一点尝试的是要记住,一旦进入你的代码while循环将持续运行,直到net_loc==null,所以lm.removeUpdates(locationListener)电话每次。

+1

你可以使用代码标签进行格式化。 – jogo