【发布时间】:2016-09-21 21:57:51
【问题描述】:
我有一个应该返回用户位置的代码,但经度不断返回null,因此我的应用程序不断崩溃。如何防止这种情况发生?
//get the user longitude and latitude
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Geocoder geocoder; //return meaningful data from user location
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);
address = addresses.get(0).getAddressLine(0);
city = addresses.get(0).getLocality();
【问题讨论】:
-
请发布您的错误堆栈跟踪
-
双经度上的空指针异常 = location.getLongitude();
-
您的
location为空,而不是您的经度。 -
lm.getLastKnownLocation可能返回 null。 -
如果它返回 null,我该如何处理这个位置
标签: java android android-location