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));
}
}
为什么我会得到nullpointerexception? loclat和loclong的值是有效的,destloc是初始化的,我没有想到它导致崩溃的原因。 此外,如果可能建议其他方法计算两点之间的距离(我已经知道distanceBetween)
答案 0 :(得分:0)
您需要初始化calcloc
变量(到目前为止显然不是这种情况)。