The App crashed before giving permissions (Google Maps API)

1

I have put in my application for the API de Google (to use the Maps) and the following lines of code so that on all devices (including those above 6.0) can give permissions and that it works correctly. The code works and a sign comes out asking for permission, you put "OK" and it starts again leaving you to use the map.

The problem is that before that poster comes a sign saying that the application has stopped and I do not want that poster to come out. What is 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 ACCESS_COARSE_LOCATION , ACCESS_FINE_LOCATION Y LOCATION_REQUEST_CODE

What came out in the log:

  

FATAL EXCEPTION: main Process: com.prueba.rudeboys.rudeboys1, PID:   6957 java.lang.SecurityException: my location requires permission   ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION at maps.ad.t.c (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.setMyLocationEna bled (Unknown   Source) at   com.google.android.gms.maps.GoogleMap.setMyLocationEnabled (U nknown   Source) at -

Updated, what I have on 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 the if was this line: map.setMyLocationEnabled(true); and now I get an error because the if I have uploaded it to the onCreate , and it has been hanging this line because it needs CheckSelfPermission, how do I do it?

    
asked by Rf Mvs 24.01.2017 в 17:04
source

1 answer

2

One reason may be that you do not have the permissions defined within the AndroidManifest.xml file:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

If this is the case, the only thing that may be happening is that the permit request is being made after trying to obtain a location on the map:

  

Did I crash the App before giving permissions (Google Maps API)?

, for example you could ask for them when starting your application inside the onCreate() method.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
            Log.i("Permisos", "Se tienen los permisos!");
        } else {
            ActivityCompat.requestPermissions(
                    this, new String[] { android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION }, 1337);
        }
    }
    
answered by 24.01.2017 / 17:51
source