我正在尝试获取用户的当前位置并将这些值放在共享首选项中。但是我在将它们放入SharedPreference时获得NPE。任何帮助都会受到赞赏。谢谢。
我的代码: -
SharedPreferences pref1=this.getSharedPreferences("LocDetails", Context.MODE_WORLD_READABLE);
LocationManager locationManager= (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new GetCurrentLocation();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
public void onLocationChanged(Location location) {
latitude= location.getLatitude();
longitude=location.getLongitude();
SharedPreferences.Editor edit=pref1.edit();
edit.putString("latitude", latitude);
edit.putString("longitude", longitude);
edit.commit();
}
错误: -
04-24 01:22:14.644: E/AndroidRuntime(7088): java.lang.NullPointerException
04-24 01:22:14.644: E/AndroidRuntime(7088): at com.sunmobileappnow.mobileappnow.GetCurrentLocation.onLocationChanged(GetCurrentLocation.java:47)
04-24 01:22:14.644: E/AndroidRuntime(7088): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:279)
04-24 01:22:14.644: E/AndroidRuntime(7088): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:208)
04-24 01:22:14.644: E/AndroidRuntime(7088): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:224)
04-24 01:22:14.644: E/AndroidRuntime(7088): at android.os.Handler.dispatchMessage(Handler.java:102)
04-24 01:22:14.644: E/AndroidRuntime(7088): at android.os.Looper.loop(Looper.java:137)
04-24 01:22:14.644: E/AndroidRuntime(7088): at android.app.ActivityThread.main(ActivityThread.java:4998)
04-24 01:22:14.644: E/AndroidRuntime(7088): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 01:22:14.644: E/AndroidRuntime(7088): at java.lang.reflect.Method.invoke(Method.java:515)
04-24 01:22:14.644: E/AndroidRuntime(7088): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
04-24 01:22:14.644: E/AndroidRuntime(7088): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
04-24 01:22:14.644: E/AndroidRuntime(7088): at dalvik.system.NativeStart.main(Native Method)
GetCurrentLocation.java
public class GetCurrentLocation extends Activity implements LocationListener{
private SharedPreferences pref1;
private String latitude,longitude;
public double lat,Long;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pref1=this.getSharedPreferences("LocDetails", Context.MODE_WORLD_READABLE);
LocationManager locationManager= (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new GetCurrentLocation();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
public void onLocationChanged(Location location) {
lat= location.getLatitude();
Long=location.getLongitude();
latitude=String.valueOf(lat);
longitude=String.valueOf(Long);
SharedPreferences.Editor edit=pref1.edit();
edit.putString("latitude", latitude);
edit.putString("longitude", longitude);
edit.commit();
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
答案 0 :(得分:1)
将实现的类传递给不创建新实例的方法;
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
答案 1 :(得分:0)
try
GPSTracker gps;
LocationManager manager = (LocationManager) getSystemService(this.LOCATION_SERVICE);
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps();
} else {
gps = new GPSTracker(myActivity.this);
if (gps.canGetLocation()) {
currentLatitude = gps.getLatitude();
currentLongitude = gps.getLongitude();
} else {
gps.showSettingsAlert();
}
}
答案 2 :(得分:0)
尝试以下代码: -
private final LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(final Location location) {
//your code here
}
};
oncreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_REFRESH_TIME,
LOCATION_REFRESH_DISTANCE, mLocationListener);
}
<强>清单强>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>