我的应用程序中有一个功能,当用户单击按钮时,它将显示最近的办公室。功能在瑞典部分地区有效,但在瑞典北部无效。我使用 try-catch 语句,我将捕获空指针异常。当我关闭网络和位置时,我收到错误消息,但在瑞典北部,没有消息,应用程序崩溃了。我的代码在下面。onFindOfficeClick() 是一个独立的函数。我怎么解决这个问题?我尝试调试但未能命中错误...不知道该怎么办。我从 xml 文件中获取所有坐标。有人有什么建议吗?
private void onFindOfficeClick() {
try {
LocationManager lm = (LocationManager)getSystemService(Service.LOCATION_SERVICE);
List<String> providersName = lm.getProviders(false);
if(providersName.size()>0){
Location location = lm.getLastKnownLocation(providersName.get(0));
float results [] = new float [1];
final double longitude = location.getLongitude();
final double latitude = location.getLatitude();
final int officeCount = mOfficesInfo.length;
double minDistance = 0;
int officeNum = 0;
for(int i=0; i<officeCount; i++){
Location.distanceBetween(latitude, longitude,
mOfficesInfo[i].getLatitude(), mOfficesInfo[i].getLongitude(), results);
if(i==0){
minDistance = results[0];
} else if(results[0]<minDistance){
minDistance=results[0];
officeNum = i;
}
}
int countryPosition = mCountries.indexOf(mOfficesInfo[officeNum].getCountry());
mCountrySpiner.setSelection(countryPosition);
mFindedOfficeName = mOfficesInfo[officeNum].getOfficeName();
mOfficesSpiner.setSelection(officeNum);
}
}
catch (NullPointerException e)
{
Toast.makeText(getApplicationContext(),"check your network and GP:s connections",Toast.LENGTH_SHORT).show();
}
}