String calculateDistanceMetric(double loclat, double loclong)
{
Log.i("Metric", String.valueOf(loclat));
Log.i("Metric", String.valueOf(loclong));
Location destloc = new Location(provider);
destloc.setLatitude(loclat);
destloc.setLongitude(loclong);
Log.i("Metric", "1");
if (calcloc.distanceTo(destloc) > 100) ////////here is where the code breaks
{
Log.i("Metric", "2");
float round = calcloc.distanceTo(destloc);
Log.i("Metric", "3");
DecimalFormat df = new DecimalFormat("##.#");
Log.i("Metric", "4");
distanceUnit = "KM";
Log.i("Metric", "5");
return String.valueOf(round/1000);
}
else
{
Log.i("Metric", "6");
distanceUnit = "Meters";
Log.i("Metric", "7");
return String.valueOf(calcloc.distanceTo(destloc));
}
}
为什么我会得到空指针异常?loclat 和 loclong 的值是有效的, destloc 已初始化,我想不出为什么会导致它崩溃。此外,如果可能建议计算两点之间距离的其他方法(我已经知道 distanceBetween)