无法找到位置应用程序崩溃

时间:2013-09-23 12:49:24

标签: android location

我正在尝试在一个小型Android应用中检测用户位置。

该应用程序之前已在运行。

但现在当我运行我的应用程序时,它只会崩溃。

这是我的代码。

private void getUserLocation() {

    location_manager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (location_manager != null) {
        provider = location_manager.getBestProvider(criteria, true);
        location = location_manager.getLastKnownLocation(provider);
        location_manager.requestLocationUpdates(provider,
                Map.MIN_TIME_BW_UPDATES,
                Map.MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
        lat = location.getLatitude();
        lang = location.getLongitude();
    }
}

堆栈跟踪

E/AndroidRuntime(10925): FATAL EXCEPTION: main
E/AndroidRuntime(10925): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.kamani.nearby/com.project.kamani.nearby.Map}: java.lang.NullPointerException
E/AndroidRuntime(10925): Caused by: java.lang.NullPointerException
E/AndroidRuntime(10925):    at com.project.kamani.nearby.Map.getUserLocation(Map.java:185)
E/AndroidRuntime(10925):    at com.project.kamani.nearby.Map.onCreate(Map.java:75)

根据我的解释,它表示我的代码中的错误位于第75行。

因此,如第75行所示,我刚刚调用了上述函数getUserLocation()

我无法理解为什么它会抛出空指针异常。

3 个答案:

答案 0 :(得分:1)

我认为,问题可能是因为位置无效。在使用它之前验证它不是null。 (在location.getLatitude()处)。


请尝试以下代码。

private void getUserLocation() {

location_manager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (location_manager != null) {

   List<String > provider=location_manager .getAllProviders();
            Location location = null;
            for(int i=0;i<provider.size();i++){
                 location=location_manager .getLastKnownLocation(provider.get(i));
                 if(location!=null){
                          location_manager.requestLocationUpdates(provider.get(i),
                                                    Map.MIN_TIME_BW_UPDATES,
                                                    Map.MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                       break;
                      }
            }


if(location!=null){
    lat = location.getLatitude();
    lang = location.getLongitude();
}else{
  // here you can initialize manually if you wish
}
}
}

答案 1 :(得分:1)

如果getLastKnownLocation没有所选提供程序的数据,则返回null。因此,如果它没有最后知道的位置,则lat和long将始终为null,因为该位置是在侦听器中设置的,它是异步的,因此您需要在onLocationChanged中设置lat和long。

您还应该转到Google Play服务中的LocationClient http://developer.android.com/training/location/index.html

答案 2 :(得分:0)

看看这个答案我正在解决同样的问题我已经解决了我的问题你可以使用这个代码

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            // First get location from Network Provider
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}
@Override
public void onLocationChanged(Location location) {
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public IBinder onBind(Intent arg0) {
    return null;
}