java - 错误 :java. lang.SecurityException:我的位置需要权限 ACCESS_FINE_LOCATION 或 ACCESS_COARSE_LOCATION

标签 java android runtime-error runtimeexception

大家好,我正在尝试获取用户的位置,但是当我请求权限时,谷歌地图无法加载。具体来说,我有这个错误:

2022-07-03 12:46:39.032 12031-12031/com.example.progetto E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.progetto, PID: 12031
    java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION
        at com.google.maps.api.android.lib6.impl.bj.J(:com.google.android.gms.dynamite_mapsdynamite@<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0634343432373536303e463434283432283735" rel="noreferrer noopener nofollow">[email protected]</a> (100700-0):10)
        at com.google.android.gms.maps.internal.i.ba(:com.google.android.gms.dynamite_mapsdynamite@<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8bb9b9b9bfbab8bbbdb3cbb9b9a5b9bfa5bab8" rel="noreferrer noopener nofollow">[email protected]</a> (100700-0):172)
        at et.onTransact(:com.google.android.gms.dynamite_mapsdynamite@<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8dbfbfbfb9bcbebdbbb5cdbfbfa3bfb9a3bcbe" rel="noreferrer noopener nofollow">[email protected]</a> (100700-0):4)
        at android.os.Binder.transact(Binder.java:914)
        at com.google.android.gms.internal.maps.zza.zzc(com.google.android.gms:play-services-maps@@18.0.0:2)
        at com.google.android.gms.maps.internal.zzg.setMyLocationEnabled(com.google.android.gms:play-services-maps@@18.0.0:3)
        at com.google.android.gms.maps.GoogleMap.setMyLocationEnabled(com.google.android.gms:play-services-maps@@18.0.0:1)
        at com.example.progetto.MapsLine.onMapReady(MapsLine.java:207)
        at com.google.android.gms.maps.zzat.zzb(com.google.android.gms:play-services-maps@@18.0.0:1)
        at com.google.android.gms.maps.internal.zzaq.zza(com.google.android.gms:play-services-maps@@18.0.0:5)
        at com.google.android.gms.internal.maps.zzb.onTransact(com.google.android.gms:play-services-maps@@18.0.0:3)
        at android.os.Binder.transact(Binder.java:914)
        at es.bc(:com.google.android.gms.dynamite_mapsdynamite@<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d3e1e1e1e7e2e0e3e5eb93e1e1fde1e7fde2e0" rel="noreferrer noopener nofollow">[email protected]</a> (100700-0):2)
        at com.google.maps.api.android.lib6.impl.bf.run(:com.google.android.gms.dynamite_mapsdynamite@<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="310303030500020107097103031f03051f0002" rel="noreferrer noopener nofollow">[email protected]</a> (100700-0):1)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

我的代码是:

public class MapsLine extends Fragment implements OnMapReadyCallback {
        private static final int MY_PERMISSION_REQUEST_ACCESS_FINE_LOCATION = 0;
        private FusedLocationProviderClient fusedLocationClient;
        private LocationCallback locationCallback;
        private boolean requestingLocationUpdates = true;
        GoogleMap gMap;
        double latitude;
        double longitude;
        @Override
        public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
                return inflater.inflate(R.layout.fragment_maps_line, container, false);
        }
        @Override
        public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
                super.onViewCreated(view, savedInstanceState);
                SupportMapFragment supportMapFragment = (SupportMapFragment)
                        getChildFragmentManager().findFragmentById(R.id.map);
                supportMapFragment.getMapAsync(this);
        }
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                Log.d("MainActivity", "onCreate");
                fusedLocationClient = LocationServices.getFusedLocationProviderClient(getActivity());
                //callback per gli aggiornamenti di posizione
                locationCallback = new LocationCallback() {
                        @Override
                        public void onLocationResult(LocationResult locationResult) {
                                if (locationResult == null) {
                                        Log.d("main_activiyt_maps", "location request is null");
                                        return;
                                }
                                //aggiorno i dati nelle textView con le informazioni della posizione aggiornata
                                for (Location location : locationResult.getLocations()) {
                                        Log.d("Location", "New Location received: " + location.toString());
                                        latitude = location.getLatitude();
                                        longitude=location.getLongitude();
                                        Log.d("main_activiyt_maps", String.valueOf(latitude));
                                        Log.d("main_activiyt_maps", String.valueOf(longitude));
                                }
                        }
                };
        }
        @Override
        public void onPause() {
                super.onPause();
                fusedLocationClient.removeLocationUpdates(locationCallback);
                requestingLocationUpdates = false;
        }
        @Override
        public void onResume() {
                super.onResume();
                Log.d("MainActivity", "onResume");
                if (checkLocationPermission()) {
                        Log.d("MainActivity", "Permessi OK");
                        //richiedo l'ultima posizione
                        //richiedo aggiornamenti sulla posizione
                        startLocationUpdates();
                }
        }
        public boolean checkLocationPermission() {
                if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                        //ho i permessi, scrivo nella casella di testo che va tutto bene
                        Log.d("Location", "The permission is granted");
                        return true;
                } else {
                        //non ho i permessi, devo richiederli
                        Log.d("Location", "Ask for permission");

                        ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSION_REQUEST_ACCESS_FINE_LOCATION);
                        return false;
                }
        }
        @Override
        public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
                switch (requestCode) {
                        case MY_PERMISSION_REQUEST_ACCESS_FINE_LOCATION: {
                                // If request is cancelled, the result arrays are empty.
                                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                                        // permission was granted
                                        Log.d("Location", "Now the permission is granted");
                                        fusedLocationClient = LocationServices.getFusedLocationProviderClient(getActivity());
                                } else {
                                        // permission was not granted
                                        Log.d("Location", "Permission still not granted");
                                }
                                break;
                        }
        }
}
        public void getLastKnownLocation() {
                if (checkLocationPermission()) {
                        fusedLocationClient.getLastLocation()
                                .addOnSuccessListener(getActivity(), new OnSuccessListener<Location>() {
                                        @Override
                                        public void onSuccess(Location location) {
                                                // Got last known location. In some rare situations this can be null.
                                                if (location != null) {
                                                        Log.d("Location", "Last known location:" + location.toString());
                                                } else {
                                                        Log.d("Location", "Last Known location not available");
                                                }
                                        }
                                });
                }
        }

        //per chiedere la posizione aggiornata
        private void startLocationUpdates() {
                if (checkLocationPermission()) {
                        requestingLocationUpdates = true;
                        LocationRequest locationRequest = LocationRequest.create();
                        locationRequest.setInterval(1000); //in ms.
                        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
                        fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper());

                }
        }
        @SuppressLint("MissingPermission")
        @Override
        public void onMapReady(@NonNull GoogleMap googleMap) {
                gMap = googleMap;
                gMap.setMyLocationEnabled(true);
                // Add a marker in Sydney and move the camera
                LatLng sydney = new LatLng(latitude,longitude);
                gMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
                gMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
                gMap.clear(); //clear old markers
        }
}

我的 list 代码是:

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
 <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="my_key" ></meta-data>

在 list 中,我有 ACCESS_COARSE_LOCATION 和 ACCESS_FINE_LOCATION,在代码中我检查权限(checkLocationPermission())。当应用程序询问我权限时,为什么它会在第一次启动时崩溃?

最佳答案

onViewCreated() 中,您正在设置 SupportMapFragment。在 onMapReady() 中,您尝试通过 gMap.setMyLocationEnabled(true); 使用位置信息。在用户授予您权限之前,请勿调用 setMyLocationEnabled()

关于java - 错误 :java. lang.SecurityException:我的位置需要权限 ACCESS_FINE_LOCATION 或 ACCESS_COARSE_LOCATION,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72845945/

相关文章:

java - 如何使用公历比较没有时间戳的日期?

java - "Refused to set unsafe header ' Cookie ' "同时以 Angular 6 发送带有 GET 请求的 cookie

android - 将图像从 fragment 传递到 Activity - 获取空对象

android - Retrofit2 不发送请求(不使用 OkHttp 进行日志记录)

sql - sql golang执行查询时无效的内存地址或nil指针取消引用

dll - “entry point not found”运行时错误的一些可能原因是什么?

java - fragment 不兼容的类型

Java 按钮全屏

android - Sencha touch 向下滚动时加载 2 个元素

java - Android.widget.RelativeLayout 无法转换为 android.widget.ImageView