简体   繁体   English

甚至在定义@NonNull之后仍为空的mLastKnownLocation

[英]empty mLastKnownLocation even after defining @NonNull

I am trying to get the device location from this piece of code 我正在尝试从这段代码中获取设备位置

private void getDeviceLocation() {
    /*
     * Get the best and most recent location of the device, which may be null in rare
     * cases when a location is not available.
     */
    try {
      if (mLocationPermissionGranted) {
        Task<Location> locationResult = mFusedLocationProviderClient.getLastLocation();
        locationResult.addOnCompleteListener(getActivity(), new OnCompleteListener<Location>() {
          @Override
          public void onComplete(@NonNull Task<Location> task) {
            if (task.isSuccessful()) {
              mMap.clear();
              // Set the map's camera position to the current location of the device.
              mLastKnownLocation = task.getResult();
              mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
                  new LatLng(mLastKnownLocation.getLatitude(),
                      mLastKnownLocation.getLongitude()), DEFAULT_ZOOM));


            } else {
              Log.d(TAG, "Current location is null. Using defaults.");
              Log.e(TAG, "Exception: %s", task.getException());
                            mMap.moveCamera(CameraUpdateFactory
                                    .newLatLngZoom(mDefaultLocation, DEFAULT_ZOOM));
                            mMap.getUiSettings().setMyLocationButtonEnabled(false);
            }
          }
        });

which generally works fine, but sometimes it throws error as: 通常可以正常工作,但有时会引发以下错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
    at SecondFragment$1.onComplete(SecondFragment.java:230)

The line 230 of the code is: 代码的第230行是:

      mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
          new LatLng(mLastKnownLocation.getLatitude(),  //Line230
              mLastKnownLocation.getLongitude()), DEFAULT_ZOOM));

I don't understand even after taking @NonNull shield, how the code goes here and gives nullpoint exception. 我什至不理解@NonNull屏蔽后,代码如何在这里出现并给出nullpoint异常。

Also, this problem occurs only in emulator. 此外,仅在仿真器中会出现此问题。 In real device, I havn't found this error (ie the app is not crashing like it does in emulator) 在真实设备中,我没有发现此错误(即应用程序没有像在模拟器中那样崩溃)

NonNull means the value returned is not null. NonNull表示返回的值不为null。 It doesn't mean that the return of a function on it (like getResult()) can't be null. 这并不意味着函数的返回值(例如getResult())不能为null。

In addition, getLastKnownLocation can always return null, no matter how you wrap it. 另外,无论如何包装,getLastKnownLocation始终可以返回null。 If you want to assure that you get a location, use getSingleUpdate and wait for the callback. 如果要确保获得位置,请使用getSingleUpdate并等待回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使在 Flutter 中定义了 errorBuilder 之后,Image.network 也会抛出错误 - Image.network throw error even after defining errorBuilder in Flutter 即使在postValue()Kotlin之后,MutableLiveData ArrayList也为空 - MutableLiveData ArrayList is empty even after postValue() Kotlin 列表即使初始化后仍然为空 - List remains empty even after being initialized ArrayList 即使在添加元素后也显示为空 - ArrayList showing empty even after adding elements Android Studio 更新后无法解析符号“NonNull” - Cannot resolve symbol 'NonNull' after Android Studio update 即使状态为 200,解析后 Json 为空 - Json Empty after parse even though status 200 Android Studio 3.2之后如何解决NonNull注释? - How to solve NonNull annotation after Android Studio 3.2? 即使字符串null或空检查后,Android Java异常 - Android Java exception even after string null or empty check Android:即使注销后ParseUser.getCurrentUser()仍返回空对象 - Android: ParseUser.getCurrentUser() returning empty object even after Logout 找不到搜索到的项目后,列表视图为空,即使我清除了搜索到的项目,列表视图仍为空 - List View empties after searched item not found and remains empty even after i clear the searched item
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM