2

Here's the exception i got :

java.lang.SecurityException
:com.google.android.gms.DynamiteModulesB line 50297 in az.c()
my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION


az : android.support.transition.TransitionIcs
c() : android.support.transition.TransitionIcs$CompatListener mCompatListener

com.google.maps.api.android.lib6.impl
:com.google.android.gms.DynamiteModulesB line 50297 in az.c()
com.google.android.gms.maps.internal
:com.google.android.gms.DynamiteModulesB line 274 in l.onTransact()
android.os
Binder.java line 387 in Binder.transact()
com.google.android.gms.maps.internal
line -1 IGoogleMapDelegate$zza$a.setMyLocationEnabled()
com.google.android.gms.maps
line -1 GoogleMap.setMyLocationEnabled()
SourceFile line 214 in MapsActivity.onMapReady()
com.google.android.gms.maps
line -1 MapFragment$a$1.zza()
com.google.android.gms.maps.internal
line -1 zzo$zza.onTransact()
android.os
Binder.java line 387 in Binder.transact()
com.google.android.gms.maps.internal
:com.google.android.gms.DynamiteModulesB line 82 in bw.a()
com.google.maps.api.android.lib6.impl
:com.google.android.gms.DynamiteModulesB line 1805 in bf.run()
android.os
Handler.java line 739 in Handler.handleCallback()
android.os
Handler.java line 95 in Handler.dispatchMessage()
android.os
Looper.java line 234 in Looper.loop()
android.app
ActivityThread.java line 5526 in ActivityThread.main()
java.lang.reflect
Method.java line -2 in Method.invoke()
com.android.internal.os
ZygoteInit.java line 726 in ZygoteInit$Method

i have a map fragment using getMap method and show the current location of user if location is enabled in device, but sometimes it gives me the above exception, i've added some location permission check in my code, but i don't know what i've missed...

thanks for help

here is some additional data :

compileSdkVersion 25
minSdkVersion 15
targetSdkVersion 25

    @Override
public void onMapReady(final GoogleMap map) {
    map.setMyLocationEnabled(true);
}
kmno
  • 85
  • 7

3 Answers3

3

Add these to your AndroidManifest.xml before application tag

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

Also for Android version >= 23, you need to request location permissions in run-time

     if (ContextCompat.checkSelfPermission(YourActivity.this,
                            Manifest.permission.ACCESS_FINE_LOCATION)
                            != PackageManager.PERMISSION_GRANTED
                            &&
                            ContextCompat.checkSelfPermission(YourActivity.this,
                                    Manifest.permission.ACCESS_COARSE_LOCATION)
                                    != PackageManager.PERMISSION_GRANTED) {
                        askForLocationPermissions();
      } else {
          //do your work
     }

And

private void askForLocationPermissions() {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.ACCESS_FINE_LOCATION)) {

            new android.support.v7.app.AlertDialog.Builder(this)
                    .setTitle("Location permessions needed")
                    .setMessage("you need to allow this permission!")
                    .setPositiveButton("Sure", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            ActivityCompat.requestPermissions(YourActivity.this,
                                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                                    LOCATION_PERMISSION_REQUEST_CODE);
                        }
                    })
                    .setNegativeButton("Not now", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
//                                        //Do nothing
                        }
                    })
                    .show();

            // Show an expanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

        } else {

            // No explanation needed, we can request the permission.
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    LOCATION_PERMISSION_REQUEST_CODE);

            // MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }

Also

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
        switch (requestCode) {
            case LOCATION_PERMISSION_REQUEST_CODE:
                if (isPermissionGranted(permissions, grantResults, Manifest.permission.ACCESS_FINE_LOCATION)) {
                    //Do you work
                } else {
                    Toast.makeText(this, "Can not proceed! i need permission" , Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

And

   public static boolean isPermissionGranted(@NonNull String[] grantPermissions, @NonNull int[] grantResults,
                                              @NonNull String permission) {
        for (int i = 0; i < grantPermissions.length; i++) {
            if (permission.equals(grantPermissions[i])) {
                return grantResults[i] == PackageManager.PERMISSION_GRANTED;
            }
        }
        return false;
    }
Atef Hares
  • 4,715
  • 3
  • 29
  • 61
0

Because there is no source code, I can just provide some information to you. If you want to access devices location, you have to check if the permission is granted and add it into AndroidManifest.xml. You can refer to this question

Community
  • 1
  • 1
petercoin
  • 53
  • 10
0

From the standard documentation from Google (and modified a bit):

https://developer.android.com/training/permissions/requesting.html

    // Assume this is the current activity, and 22 is app specific chose your favorite number and define it in your app somewhere as a final int
    // Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.ACCESS_FINE_LOCATION)) {

            // Show an explanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

        } else {

            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    22);

            // ACCESS_FINE_LOCATION is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        }
    }

Where you also need:

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 22: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}
Lloyd Rochester
  • 689
  • 8
  • 7