我有一個問題,我試圖創建一個使用位置管理器但不是當前正在運行的活動的類。所以當我從任何活動中調用這個類時,它都會在後臺完成。我試過擴展服務,但一直返回空指針錯誤。Android位置管理器返回空指針異常
這是這行代碼返回錯誤。
public class BackgroundGps extends Service{
UserObject uo = null;
BackgroundGps(UserObject userObj){
uo = userObj;
uo.setHeader("GPSUpdate");
}
public void locationListener(){
LocationManager mlocManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new LocationListener(){
public void onLocationChanged(Location location){
if(uo.getLatitude() == ("" + location.getLatitude()) && uo.getLongitude() == ("" + location.getLongitude())){}
else{
uo.setLatitude("" + location.getLatitude());
uo.setLongitude("" + location.getLongitude());
updateGPS();
}
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 5, mlocListener);
mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 5, mlocListener);
}
感謝
你在仿真器或設備上運行嗎? – Sid 2012-03-21 19:16:20
當前正在設備上運行。 – Arran 2012-03-21 19:45:10