0
 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)

4

1 回答 1

0

您需要初始化calcloc变量(到目前为止显然不是这种情况)。

于 2013-02-11T08:39:07.590 回答