android - LocationManager throws NullPointerExpection -


please have code has been working me while, of sudden not working more, returns null pointer exception at

double lat = (double) (lastknownlocation.getlatitude());. 

i have set required permission @ manifest file , seems not work again. think there must problem using 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);        // 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 , if not send user gsp settings     // better solution display dialog , suggesting      // go 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 @ startup */   @override   protected void onresume() {     super.onresume();     locationmanager.requestlocationupdates(locationmanager.gps_provider, 400, 1, this);   }    /* remove locationlistener updates when activity 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)); 

lastknownlocation return null if either location has old or if provider has never been turned on. must take account. way sure valid location request updates , in onlocationchanged.

also, lastknownlocation may return value that's wrong- 10 or 15 minutes ago possible. can use it, don't expect accuracy.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -