java - 在获取当前纬度和经度时获取空对象引用

标签 java android google-maps location

我正在为我的项目获取当前的纬度和经度,它一开始工作得很好,但现在我正在检索空值。我确实已打开位置并授予物理设备中位置的权限,但仍然得到空值。 我的代码是

mMap.setMyLocationEnabled(true);
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    Activity#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for Activity#requestPermissions for more details.
            return;
        }

        Location location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
        double cLatitude = location.getLatitude();
        double cLongitude = location.getLongitude();
        LatLng currentLocation = new LatLng(cLatitude, cLongitude);
        System.out.println("Current Location : "+cLatitude+", "+cLongitude);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(cLatitude, cLongitude), 13f));

我收到的错误是

java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference

最佳答案

添加LocationListener以获取当前位置。

private void getLocation() {
      // Get the location manager
      LocationManager locationManager = (LocationManager) 
          getSystemService(LOCATION_SERVICE);
      Criteria criteria = new Criteria();
      String bestProvider = locationManager.getBestProvider(criteria, false);
      Location location = locationManager.getLastKnownLocation(bestProvider);
      LocationListener loc_listener = new LocationListener() {

        public void onLocationChanged(Location l) {}

        public void onProviderEnabled(String p) {}

        public void onProviderDisabled(String p) {}

        public void onStatusChanged(String p, int status, Bundle extras) {}
      };
      locationManager
          .requestLocationUpdates(bestProvider, 0, 0, loc_listener);
      location = locationManager.getLastKnownLocation(bestProvider);
      try {
        lat = location.getLatitude();
        lon = location.getLongitude();
      } catch (NullPointerException e) {

      }
    }

关于java - 在获取当前纬度和经度时获取空对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60771713/

相关文章:

java - Backendless - 将对象保存为父类

java - admob 横幅位置/通知栏

Android 替换system/lib/lib.so 不起作用

android - 谷歌地方 API Android : Autocompletion closes too quickly

java - 为了在 Java 中整合 + 一个条件

java - 使用 MongoDB runCommand 方法

java - Javafx web View 中的 JSException

java - 使用jackson进行JSON反序列化

javascript - 原型(prototype),Google Maps Ajax API V3 - 错误但不确定原因。 - g.e 未定义

android - Google Maps Geocoding API key 使用计数