Android Question GoogleMaps App Crash Mystery

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
I wonder if anyone can help with a problem I am having.

I have an app which is using GoogleMaps within an activity.
In the activity_create I have
B4X:
  Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)

    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Not(Result) Then
        Msgbox("You need to approve Location Permissions to view the map",Starter.gstrApplicationTitle)
        Activity.Finish
    End If

In the Map_ready event I have
B4X:
        gmap = mfMap.GetMap
    gmap.MyLocationEnabled = True
    gmap.MapType = gmap.MAP_TYPE_NORMAL

However, I am getting crash reports in Crashlytics which say the following:
Fatal Exception: java.lang.SecurityException
my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION

com.google.maps.api.android.lib6.impl.bf.c :)com.google.android.gms.dynamite_mapsdynamite@14366051@14.3.66 (040408-213742215):551)

com.google.android.gms.maps.internal.l.a :)com.google.android.gms.dynamite_mapsdynamite@14366051@14.3.66 (040408-213742215):277)


fq.onTransact :)com.google.android.gms.dynamite_mapsdynamite@14366051@14.3.66 (040408-213742215):4)

android.os.Binder.transact (Binder.java:628)

com.google.android.gms.internal.maps.zza.transactAndReadExceptionReturnVoid (Unknown Source:7)

com.google.android.gms.maps.internal.zzg.setMyLocationEnabled (Unknown Source:9)

com.google.android.gms.maps.GoogleMap.setMyLocationEnabled (Unknown Source:2)

anywheresoftware.b4a.objects.MapFragmentWrapper$GoogleMapWrapper.setMyLocationEnabled (MapFragmentWrapper.java:307)

uk.co.digitwell.interdirect.mkccm.parkmycar._mfmap_ready (parkmycar.java:1377)

The crash seems to suggest that the user did not approve the ACCESS_FINE_LOCATION permission, which is odd because the Activity_Create should exit.

Does anyone have any ideas why this is happening?

Thanks
 

DonManfred

Expert
Licensed User
Longtime User
DO NOT set the property in the designer (EnableMyLocation or so).
Wait for the answer in the RP-Call and set it later in Map_ready event. But only if you HAVE permission....
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Manfred that is exactly what I am doing.

In the designer, the My Location Enabled is not set.

upload_2018-11-6_15-6-41.png


It doesn't happen to everyone who runs the app, just on the very odd occasion.
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Ah, I think I have it! @DonManfred your comment gave me a clue.

In the Activity_create I have already loaded the layout, with the Map Fragment BEFORE calling CheckAndRequest.

If the User, takes a long time to decide to allow or refuse the permission, the Map_ready event is fired, whilst waiting for the PermissionResult.

Now when the code calls
gmap.MyLocationEnabled = True

in the map ready event, the permission has not been approved.

Solution:
I think I need to move the permission check to before the LoadLayout and all will be fine.

What do you think?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Now when the code calls
gmap.MyLocationEnabled = True

in the map ready event, the permission has not been approved.

Only set this property IF you HAVE permission. Use a global flag to remember. If you do not have permission; do not change the property.
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Yes, I will include that as well, but I think the problem was events firing out of the expected order.

Expected:
Activity Create
Application_PermissionResult
Map_ready​

Actual:
Actvity_create
Map_ready <-- this event arrives before expected because the user does not click on the permission dialog for a long time.
Application_permissionResult - Too late now!:oops:
If the user refuses the permission I close the activity.
 
Upvote 0
Top