【问题标题】:LocationManager throws NullPointerExpectionLocationManager 抛出 NullPointerExpection
【发布时间】:2014-05-31 14:43:56
【问题描述】:

请给我这段一直为我工作的代码,突然间它不再工作了,它返回null pointer exception@

double lat = (double) (lastKnownLocation.getLatitude());.

我已在清单文件中设置了所有必需的权限,但它似乎不再起作用。我想现在使用 GPS 提供商肯定有问题。

    latituteField = (TextView) findViewById(R.id.TextViewLatitudeValue);
    longitudeField = (TextView) findViewById(R.id.TextViewLongitudeValue);
    SpeedField = (TextView) findViewById(R.id.textViewSpeedValue);
    AltitudeField = (TextView) findViewById(R.id.textViewAltitudeValue);        
    AddressLabel=(TextView)findViewById(R.id.textViewAddress);
    lbllatitude=(TextView)findViewById(R.id.lblLatDMS);
    lbllongitude=(TextView)findViewById(R.id.lblLongDMS);



    // Get the location manager
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // GPS_PROVIDER
    String locationProvider = LocationManager.GPS_PROVIDER;  
    Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);

    LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
    boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);

    // check if enabled and if not send user to the GSP settings
    // Better solution would be to display a dialog and suggesting to 
    // go to the settings
    if (!enabled) 
    {           
      Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
      startActivity(intent);

    }
    else if (locationProvider!= null) 
    {
     Toast.makeText(this, locationProvider +" has been selected",Toast.LENGTH_SHORT).show();
      onLocationChanged(lastKnownLocation);
    } 
    else 
    {
      latituteField.setText("0.00");
      longitudeField.setText("0.00");
      AltitudeField.setText("0.00");
      SpeedField.setText("0.00");

    }

  }

  /* Request updates at startup */
  @Override
  protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 1, this);
  }

  /* Remove the locationlistener updates when Activity is paused */
  @Override
  protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
  }

  @Override
  public void onLocationChanged(Location lastKnownLocation) {

    double lat = (double) (lastKnownLocation.getLatitude());
    double lng = (double) (lastKnownLocation.getLongitude());
    double Alt = (double) (lastKnownLocation.getAltitude());
    double Speed = (double) (lastKnownLocation.getSpeed());

    latituteField.setText(String.format("%.5f",lat));
    longitudeField.setText(String.format("%.5f",lng));
    AltitudeField.setText(String.format("%.5f",Alt));
    SpeedField.setText(String.format("%.5f",Speed));

【问题讨论】:

  • 做一件事,去尝试在开阔的天空中运行您的应用程序...这将修复您的 gps...尝试一下..

标签: android android-location


【解决方案1】:

如果LastKnownLocation 的位置太旧或从未打开过提供程序,则LastKnownLocation 将返回null。您必须考虑到这一点。确保获得有效位置的唯一方法是请求更新并将其放入 onLocationChanged。

另外,lastKnownLocation 可能返回一个非常错误的值 - 10 或 15 分钟前的某些事情是可能的。您可以使用它,但不要期望准确性。

【讨论】:

  • 我再次尝试运行它后,它开始工作,并且空异常不再出现
  • 因为现在无论出于何种原因,它都有一个非陈旧的位置可以返回。您仍然需要考虑 getLastKnownLocation 可能返回 null。
  • 这是否意味着,我使用这个位置管理器的结构是错误的?如何防止这种情况再次发生
  • 主要你只需要确保在使用前检查lastKnownLocation是否为null。否则你的代码很好——你不能假设有一个有效的 lastKnownLocation。
猜你喜欢
  • 2023-03-26
  • 2020-08-23
  • 2015-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-25
  • 2017-01-05
相关资源
最近更新 更多