Question

I am trying to fetch the current location of the user and put those values in Shared preferences .But I am getting NPE while putting them into the SharedPreferences.Any help would be appreciated.Thanks.

My code :-

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();

           }

Error:-

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) {

        }   

}
Was it helpful?

Solution

pass the implemented class to the method not creating a new instance of it;

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

OTHER TIPS

 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();

        }
    }

try below code:-

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);
}

manifest

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>


<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top