NullPointerException Location.getLongitude()

Posted

技术标签:

【中文标题】NullPointerException Location.getLongitude()【英文标题】: 【发布时间】:2015-06-10 21:16:46 【问题描述】:

我试图在我的片段中的 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();

【问题讨论】:

似乎 lastKnownLocation 无法确定,所以它给你null,因此你可以使用任何Location 方法,直到你真正得到Location Location Manager Returns Null Pointer Expection的可能重复 添加一些日志以便于理解。 【参考方案1】:

你没有错。

location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

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

【讨论】:

【参考方案2】:

使用 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" />

【讨论】:

【参考方案3】:

您是在谈论回调之外的location.getLongitude() 调用吗? 如果这就是您所说的,则无法保证手机将具有 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;
  

【讨论】:

是的,我说的是回调之外的 location.getLongitude()

以上是关于NullPointerException Location.getLongitude()的主要内容,如果未能解决你的问题,请参考以下文章

vue中引入高德地图Loca数据可视化

12月23相约Loca—Marvin Ramos salsa之夜

NullPointerException:createProgressDialog

Python 死锁现象

vue中引入高德地图Loca数据可视化

vue中引入高德地图Loca数据可视化