how could this work with eclipse, but not in an actual phone?
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(provider);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Just after that i take the latitude and the longitude to put in on a server, but it 开发者_开发技巧crash my phone.
Settings>Location and Security>My Location
service is not enabled into your Android device
hence yourlocationManager.getLastKnownLocation(provider)
( If the provider is currently disabled, null is returned.) is producing null value, Later then you are invoking double latitude = location.getLatitude(); double longitude = location.getLongitude();
with a null value object, so your application is crashing.
精彩评论