2011-04-14 27 views
1

我試圖定期更新手機的位置,但我得到了一些與我的代碼錯誤。那就是:試圖獲取Android手機的更新位置,但代碼給我的問題

LocationManager locm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = locm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

double longitude = location.getLongitude(); 
double latitude = location.getLatitude(); 



private final LocationListener locationListener = new LocationListener() { 
    public void onLocationChanged(Location location) { 
     longitude = location.getLongitude(); 
     latitude = location.getLatitude(); 
    } 

    locm.requestLocationUpdates(LocationManager.GPS, 2000, 10, locationListener); 
     // error here : Syntax error on token(s), misplaced constructs 

的事情是,當我拿出的代碼的最後一行,也有在代碼中沒有錯誤。請有人幫我嗎?任何幫助將非常感激。

回答

0

您錯過了新位置偵聽器中的右括號。

private final LocationListener locationListener = new LocationListener() { 
    public void onLocationChanged(Location location) { 
     longitude = location.getLongitude(); 
     latitude = location.getLatitude(); 
    } 
}; <-- 
+0

謝謝,但現在我得到在同一行上兩個錯誤,他說:。(「令牌語法錯誤‘’,<預期」和「令牌語法錯誤,ConstructorHeaderName預期,而不是」是什麼這意味着什麼? – 2011-04-14 23:30:53

+0

這意味着你正試圖在任何方法/初始化塊外執行一個方法,它與這一行有關:「locm.requestLocationUpdates(LocationManager.GPS,2000,10,locationListener);」如果你想執行它在任何方法之外您將需要使用初始化塊。在這裏看一些例子:http://www.java2s.com/Tutorial/Java/0100__Class-Definition/0140__Initialization-Block.htm – MByD 2011-04-14 23:37:12

0

您的最後一行(「locm.resquestLocationUpdates ...」)需要在方法中。它看起來像直接在一個班級中。