LocationManager抛出NullPointerExpection [英] LocationManager throws NullPointerExpection

查看:72
本文介绍了LocationManager抛出NullPointerExpection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请提供这段代码,这对我一直起作用,突然间,它不再起作用,它在

返回null pointer exception

Please I have this code which has been working for me all this while, all of a sudden it is not working any more, it returns null pointer exception at

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

我已在清单文件中设置了所有必需的权限,但似乎无法再次使用.我认为现在使用GPS提供程序肯定有问题.

I have set all required permission at the manifest file , but it seems not to work again. I think there must be a problem using the GPS provider now.

    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));

推荐答案

LastKnownLocation如果所在的位置太旧或提供者从未开启过,则将返回null.您必须考虑到这一点.确保获取有效位置的唯一方法是请求更新并将其保存在onLocationChanged中.

LastKnownLocation will return null if either the location it has is too old or if the provider has never been turned on. You must take this into account. THe only way to be sure to get a valid location is to request updates and get it in onLocationChanged.

此外,lastKnownLocation可能会返回非常错误的值-可能在10或15分钟前出现错误.您可以使用它,但不要期望准确性.

ALso, lastKnownLocation may return a value that's very wrong- something from 10 or 15 minutes ago is possible. You can use it, but don't expect accuracy.

这篇关于LocationManager抛出NullPointerExpection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆