尝试应用时出现奇怪的错误

时间:2015-10-08 11:15:27

标签: android

我正在尝试输入我在以下主题中找到的代码:

How do I get the current GPS location programmatically in Android?

我选择了第二个解决方案并输入代码:

   private Location getLastBestLocation() {
        Location locationGPS = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        Location locationNet = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        long GPSLocationTime = 0;
        if (null != locationGPS) { GPSLocationTime = locationGPS.getTime(); }

        long NetLocationTime = 0;

        if (null != locationNet) {
            NetLocationTime = locationNet.getTime();
        }

        if ( 0 < GPSLocationTime - NetLocationTime ) {
            return locationGPS;
        }
        else {
            return locationNet;
        }
    }
static final int TWO_MINUTES = 1000 * 60 * 2;


@override
public void onLocationChanged(Location location) {

    makeUseOfNewLocation(location);
    if(currentBestLocation == null){
        currentBestLocation = location;
    }

    ....
}


/**
 * This method modify the last know good location according to the arguments.
 *
 * @param location The possible new location.
 */
void makeUseOfNewLocation(Location location) {
    if ( isBetterLocation(location, currentBestLocation) ) {
        currentBestLocation = location;
    }
}

....

/** Determines whether one location reading is better than the current location fix
 * @param location  The new location that you want to evaluate
 * @param currentBestLocation  The current location fix, to which you want to compare the new one.
 */
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
    if (currentBestLocation == null) {
        // A new location is always better than no location
        return true;
    }

    // Check whether the new location fix is newer or older
    long timeDelta = location.getTime() - currentBestLocation.getTime();
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
    boolean isNewer = timeDelta > 0;

    // If it's been more than two minutes since the current location, use the new location,
    // because the user has likely moved.
    if (isSignificantlyNewer) {
        return true;
        // If the new location is more than two minutes older, it must be worse.
    } else if (isSignificantlyOlder) {
        return false;
    }

    // Check whether the new location fix is more or less accurate
    int accuracyDelta;
    accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
    boolean isLessAccurate = accuracyDelta > 0;
    boolean isMoreAccurate = accuracyDelta < 0;
    boolean isSignificantlyLessAccurate = accuracyDelta > 200;

    // Check if the old and new location are from the same provider
    boolean isFromSameProvider = isSameProvider(location.getProvider(),
            currentBestLocation.getProvider());

    // Determine location quality using a combination of timeliness and accuracy
    if (isMoreAccurate) {
        return true;
    } else if (isNewer && !isLessAccurate) {
        return true;
    } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
        return true;
    }
    return false;
}

/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
    if (provider1 == null) {
        return provider2 == null;
    }
    return provider1.equals(provider2);
}
}

}

因为我缺乏声誉级别,所以我不能直接询问线程,因此这个帖子。

Android Studio说

'mLocationManager'

'currentBestLocation'

未定义。

你能弄明白它有什么问题吗?谢谢!

另外,对于旁注,我试图使这个应用程序工作,以便我可以修改它每10秒循环并存储变量来计算速度

2 个答案:

答案 0 :(得分:0)

您需要定义这些字段,例如错误提及。

private Location currentBestLocation = null;

private mLocationManager = null;

// In onCreate, or something similar
mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

答案 1 :(得分:0)

Android Studio会生成缺少的代码。当您按住红色标记的问题时,左侧会显示一个符号。通过单击该符号,您可以选择其中一种可能性来生成所需的缺失代码。

此处有'mLocationManager''currentBestLocation'缺失的声明。因此Android Studio无法解决它们。在onCreate()方法之前创建它们,如:

private mLocationManager;
private Location currentBestLocation;

并设置onCreate():

mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

currentBestLocation = new Location;