0

Please I have this code which has been working for me all this while, all of a sudden it is not working any more, it returns null pointer exception at

double lat = (double) (lastKnownLocation.getLatitude());.

I have set all required permission at the manifest file , but it seems not to work again. I think there must be a problem using the GPS provider now.

    latituteField = (TextView) findViewById(R.id.TextViewLatitudeValue);
    longitudeField = (TextView) findViewById(R.id.TextViewLongitudeValue);
    SpeedField = (TextView) findViewById(R.id.textViewSpeedValue);
    AltitudeField = (TextView) findViewById(R.id.textViewAltitudeValue);        
    AddressLabel=(TextView)findViewById(R.id.textViewAddress);
    lbllatitude=(TextView)findViewById(R.id.lblLatDMS);
    lbllongitude=(TextView)findViewById(R.id.lblLongDMS);



    // Get the location manager
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // GPS_PROVIDER
    String locationProvider = LocationManager.GPS_PROVIDER;  
    Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);

    LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
    boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);

    // check if enabled and if not send user to the GSP settings
    // Better solution would be to display a dialog and suggesting to 
    // go to the settings
    if (!enabled) 
    {           
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
      startActivity(intent);

    }
    else if (locationProvider!= null) 
    {
     Toast.makeText(this, locationProvider +" has been selected",Toast.LENGTH_SHORT).show();
      onLocationChanged(lastKnownLocation);
    } 
    else 
    {
      latituteField.setText("0.00");
      longitudeField.setText("0.00");
      AltitudeField.setText("0.00");
      SpeedField.setText("0.00");

    }

  }

  /* Request updates at startup */
  @Override
  protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 1, this);
  }

  /* Remove the locationlistener updates when Activity is paused */
  @Override
  protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
  }

  @Override
  public void onLocationChanged(Location lastKnownLocation) {

    double lat = (double) (lastKnownLocation.getLatitude());
    double lng = (double) (lastKnownLocation.getLongitude());
    double Alt = (double) (lastKnownLocation.getAltitude());
    double Speed = (double) (lastKnownLocation.getSpeed());

    latituteField.setText(String.format("%.5f",lat));
    longitudeField.setText(String.format("%.5f",lng));
    AltitudeField.setText(String.format("%.5f",Alt));
    SpeedField.setText(String.format("%.5f",Speed));
earthw0rmjim
  • 19,027
  • 9
  • 49
  • 63
Kwaadjei
  • 309
  • 1
  • 5
  • 18
  • do one thing,go and try running your app in open sky...this will fix your gps...try it.. – Lal May 31 '14 at 15:52

1 Answers1

0

LastKnownLocation will return null if either the location it has is too old or if the provider has never been turned on. You must take this into account. THe only way to be sure to get a valid location is to request updates and get it in onLocationChanged.

ALso, lastKnownLocation may return a value that's very wrong- something from 10 or 15 minutes ago is possible. You can use it, but don't expect accuracy.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • After I tried running it again, it started working and the null exception is not coming again – Kwaadjei May 31 '14 at 15:21
  • Because now for whatever reason it has a non-stale location to return. You still have to account for getLastKnownLocation to possibly return null. – Gabe Sechan May 31 '14 at 15:22
  • Does it mean , the structure of how I am using this location manager is wrong? How to I prevent this from occurring again – Kwaadjei May 31 '14 at 15:42
  • Mainly you just need to make sure to check if lastKnownLocation is null before using it. Otherwise your code is fine- you just can't assume that there is a valid lastKnownLocation. – Gabe Sechan May 31 '14 at 15:48