猿问

错误在空对象引用上调用虚拟方法'double android.location.Location

我要尝试做的就是让用户获得他喜欢的类型的位置的列表。例如,如果输入是医院,我的应用程序将使用搜索字符串“ Hospital”打开google maps。但是,正如文档中所建议的那样,使用geo:0,0?q=hospitaluri等地理编码显示了所有坐标0纬度和0经度附近的医院。因此,当我尝试使用以下代码首先获取用户坐标时。

问题在于,始终位置始终返回null。我知道一个事实,只要启用了位置,另一个应用程序Jarvis就可以轻松地完成任务,如果没有的话,仅仅是要求用户这样做。但是在相同条件下,我的应用程序总是崩溃。

我做了什么:

  1. 白天和黑夜都试图解决此问题。

  2. 浏览了有关该主题的大量页面。

  3. 尝试了自己的不同代码。

而且它仍然会发生。我是Android的初学者。并且请帮我。有没有办法解决我的问题?甚至以为其他应用程序可以设法做到这一点。

同时检查位置!= null需要ACCESS_FINE_LOCATION权限,该权限使我的应用程序混乱,导致其始终返回为null。


凤凰求蛊
浏览 695回答 3
3回答

沧海一幻觉

这是执行此操作的另一种方法,您已经使用了保险丝定位概念,并且对我来说是成功的mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);是声明融合的位置&nbsp;@SuppressWarnings( {"MissingPermission"})private void enableLocationComponent() {&nbsp; &nbsp; System.out.println("on map click is here in permission///////////////");&nbsp; &nbsp; // Check if permissions are enabled and if not request&nbsp; &nbsp; if (PermissionsManager.areLocationPermissionsGranted(this)) {&nbsp; &nbsp; &nbsp; &nbsp; // Activate the MapboxMap LocationComponent to show user location&nbsp; &nbsp; &nbsp; &nbsp; // Adding in LocationComponentOptions is also an optional parameter&nbsp; &nbsp; &nbsp; &nbsp; LocationComponent locationComponent = mapboxMap.getLocationComponent();&nbsp; &nbsp; &nbsp; &nbsp; locationComponent.activateLocationComponent(this);&nbsp; &nbsp; &nbsp; &nbsp; locationComponent.setLocationComponentEnabled(true);&nbsp; &nbsp; &nbsp; &nbsp; // Set the component's camera mode&nbsp; &nbsp; &nbsp; &nbsp; locationComponent.setCameraMode(CameraMode.TRACKING);&nbsp; &nbsp; &nbsp; &nbsp; mFusedLocationClient.getLastLocation().addOnSuccessListener(this, new OnSuccessListener<Location>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onSuccess(Location location) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; originLocation=location;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Got last known location. In some rare situations this can be null.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (location != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; originLocation=location;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(" permission granted location is in iff ++///////////////"+location);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; //originLocation = locationComponent.getLastKnownLocation();&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("origin location is that//////////"+originLocation);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; permissionsManager = new PermissionsManager(this);&nbsp; &nbsp; &nbsp; &nbsp; permissionsManager.requestLocationPermissions(this);&nbsp; &nbsp; }}@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {&nbsp; &nbsp; permissionsManager.onRequestPermissionsResult(requestCode, permissions, grantResults);&nbsp; &nbsp; Toast.makeText(this, "granted", Toast.LENGTH_LONG).show();}@Overridepublic void onExplanationNeeded(List<String> permissionsToExplain) {&nbsp; &nbsp; Toast.makeText(this, R.string.user_location_permission_explanation, Toast.LENGTH_LONG).show();}@Overridepublic void onPermissionResult(boolean granted) {&nbsp; &nbsp; if (granted) {&nbsp; &nbsp; &nbsp; &nbsp; enableLocationComponent();&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(this, R.string.user_location_permission_not_granted, Toast.LENGTH_LONG).show();``&nbsp; &nbsp; &nbsp; &nbsp; finish();&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Android
Java
我要回答