空指针异常 Location.getLongitude()

NullPointerException Location.getLongitude()

我正在尝试在片段中的 onCreateView() 中获取当前位置。我在尝试获取当前经度的行中遇到空指针异常。我做错了什么?

final LocationListener locationListener = new LocationListener()
        {
            @Override
            public void onLocationChanged(Location currentLocation)
            {
                latitude = currentLocation.getLatitude();
                longitude = currentLocation.getLongitude();
            }
            public void onProviderDisabled(String provider)
            {

            }
            public void onProviderEnabled(String provider)
            {

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

            }
        };

        lm = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 1, locationListener);
        location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        longitude = location.getLongitude();
        latitude = location.getLatitude();

你没有看错。

location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

该方法 return 仅 最后已知位置 。它不计算位置。当没有旧位置记录时为空。您请求更新您的代码。因此,如果您需要,可以等到位置更新。

您是在谈论回调之外的 location.getLongitude() 调用吗? 如果这就是您所说的,则无法保证 phone 将具有 GPS 或启用 GPS。在任何一种情况下,您都会得到一个空响应。虽然很难满足所有情况,但您最好从很可能存在的网络提供商处获取位置信息。此外,我会使用 Criteria 构建器来获得更适合您需要的连接。例如,如果你只想知道用户在哪个城市,GPS 就完蛋了。

//force last known location from network provider
String providerName = LocationManager.NETWORK_PROVIDER;
if (mLocationManager.isProviderEnabled(providerName)) {
  Location lastLocation = mLocationManager.getLastKnownLocation(providerName);
}

//get high accuracy provider
providerName = mLocationManager.getBestProvider(createMediumCriteria(), true);
if (providerName == null) {
  // get low accuracy provider
  providerName = mLocationManager.getBestProvider(createLowCriteria(), true);
}

if (providerName == null) {
  //get low accuracy even if its disabled
  providerName = mLocationManager.getBestProvider(createLowCriteria(), false);
}

if (providerName != null && mLocationManager.isProviderEnabled(providerName)) {

  Location lastLocation = mLocationManager.getLastKnownLocation(providerName);

  if (lastLocation != null) {
    onLocationChanged(lastLocation);
  }
  //else {
  //timeout after 30 seconds.
  if (!sLocationHandler.hasMessages(MSG_UNREGISTER)) {
    sLocationHandler.sendEmptyMessageDelayed(MSG_UNREGISTER, 30000);
  }

  //LocationProvider provider = locMgr.getProvider(providerName);
  // using low accuracy provider... to listen for updates
  mLocationManager.requestLocationUpdates(providerName, 0, 0f, this);
  //}

标准构建器类似于:

  /**
   * this criteria will settle for less accuracy, high power, and cost
   */
  public static Criteria createLowCriteria() {

    Criteria c = new Criteria();
    c.setAccuracy(Criteria.ACCURACY_LOW);
    c.setAltitudeRequired(false);
    c.setBearingRequired(false);
    c.setSpeedRequired(false);
    c.setCostAllowed(true);
    c.setPowerRequirement(Criteria.POWER_LOW);
    return c;
  }

  /**
   * this criteria needs high accuracy, high power, and cost
   */
  public static Criteria createMediumCriteria() {

    Criteria c = new Criteria();
    c.setAccuracy(Criteria.ACCURACY_MEDIUM);
    c.setAltitudeRequired(false);
    c.setBearingRequired(false);
    c.setSpeedRequired(false);
    c.setCostAllowed(true);
    c.setPowerRequirement(Criteria.POWER_HIGH);
    return c;
  }

使用 NETWORK_PROVIDER 获取位置,因为很多时候 GPS 会给出空的纬度和经度

   m_locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, this);
    m_location = m_locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

在清单中添加以下权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />