V
V
Vadim Popov2018-06-19 15:49:41
Android
Vadim Popov, 2018-06-19 15:49:41

How to properly call Permission to work with geolocation?

Good afternoon everyone, at the moment when I ask the user for permission to work with geolocation, the application stops working.

private static final int PERMISSION_REQUEST = 1;
@Override
    protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST);
        }else{
            setCurrentGeo();
        }
     }

Here is what LogCat says

06-19 12:30:04.284 20047-20047/popovvad.findme E/AndroidRuntime: FATAL EXCEPTION: main Process: popovvad.findme, PID: 20047 java.lang.RuntimeException: Unable to start activity ComponentInfo{popovvad.findme/popovvad. findme.MapActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.yandex.mapkit.mapview.MapView.onStart()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2778) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect .Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: java. lang.NullPointerException: Attempt to invoke virtual method 'void com.yandex.mapkit.mapview.MapView.onStart()' on a null object reference at popovvad.findme.MapActivity.onStart(MapActivity.java:121) at android.app. Instrumentation.callActivityOnStart(Instrumentation.java:1334) at android.app.Activity.performStart(Activity.java:7029) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2741) at android.app.ActivityThread.handleLaunchActivity(ActivityThread .java:2856) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 06-19 12:30:04.316 20047-20085/popovvad.findme E/com.yandex.runtime.sensors.internal.LastKnownLocation: failed to get last known location: java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.internal.os.ZygoteInit.main(ZygoteInit.java:807) 06-19 12:30:04.316 20047-20085/popovvad.findme E/com.yandex.runtime.sensors.internal.LastKnownLocation: failed to get last known location: java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.internal.os.ZygoteInit.main(ZygoteInit.java:807) 06-19 12:30:04.316 20047-20085/popovvad.findme E/com.yandex.runtime.sensors.internal.LastKnownLocation: failed to get last known location: java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission.

Crashes on a line with a condition
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)

At the same time, if there is permission for geolocation, then the application goes on as expected and works properly, so the question arose how to correctly call the permission dialog in this case? Another note, after the application has completed the robot, a dialog appears to allow the application to work with geolocation.
Permissions in the manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Chvarkov, 2018-06-19
@vadimpopov94

In your case (judging by the error log), the problem is not in the permission request, but in the fact that your map is trying to get coordinates when the user has not yet given the green light to gps

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question