I have put in my application for the API de Google
(to use the Maps) and the following lines of code so that in all the devices (including those superior to 6.0) they can give permissions and that it works correctly. The code works and a sign appears asking for permission, you put "OK" and it starts again letting you use the map.
The problem is that before that banner a banner appears saying that the application has stopped and I don't want that banner to appear. Missing??
Part of the code where the permissions are:
if (Build.VERSION.SDK_INT >= 23) {
if (ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
} else {
ActivityCompat.requestPermissions(
getActivity(), new String[] { android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION }, 1337);
}
}
Permissions that I have in the manifest are among others:
the of ACCESS_COARSE_LOCATION
, ACCESS_FINE_LOCATION
andLOCATION_REQUEST_CODE
What I got in the log:
FATAL EXCEPTION: main Process: com.test.rudeboys.rudeboys1, PID: 6957 java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION at maps.ad.tc(Unknown Source) at xj.onTransact(:com.google. android.gms.DynamiteModulesB:274) at android.os.Binder.transact(Binder.java:387) at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.setMyLocationEnabled(Unknown Source ) at com.google.android.gms.maps.GoogleMap.setMyLocationEnabled(Unknown Source) at –
Updated, what I have in the onCreate:
public class Map_fragment extends Fragment implements OnMapReadyCallback {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_map, container, false);
if ( Build.VERSION.SDK_INT >= 23) {
if (ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
} else {
ActivityCompat.requestPermissions(
getActivity(), new String[] { android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION }, 1337);
}
}
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// MapFragment mapFragment = (MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.mapid);
// mapFragment.getMapAsync(this);
// USO SUPPORTMAPFRAGMENT PORQUE SOLO CON MAPFRAGMENT SE ME CRASHEABA AL VOLVER A ENTRAR AL MAPA!!
SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.mapid);
mapFragment.getMapAsync(this);
return v;
}
But below if
it was this line: map.setMyLocationEnabled(true);
and now it gives me an error because the if I have uploaded it above to onCreate , and this line has been left hanging because it needs the checkSelfPermission, how do I do it?
One reason may be that you don't have the permissions defined within the AndroidManifest.xml file:
If this is the case, the only thing that may be happening is that the permission request is being made after trying to obtain a location on the map:
, for example you could request them when starting your application inside the
onCreate()
.