1

I have a problem with provider apparently, my app crashes on launch although i enabled COARSE and FINE location permissions in my manifest. I have also INTERNET permission added.

My code :

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    provider = locationManager.getBestProvider(new Criteria(), false);

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(
                this,
                new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION},
                PERMISSION_LOCATION_REQUEST_CODE);
    }
    locationManager.requestLocationUpdates(provider, 400, 1, this);

And here is the logcat:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.project.korsa.korsa, PID: 32462
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.korsa.korsa/com.project.korsa.korsa.YourLocation}: java.lang.IllegalArgumentException: invalid provider: null
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3305)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3401)
                  at android.app.ActivityThread.access$1100(ActivityThread.java:229)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:7303)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
               Caused by: java.lang.IllegalArgumentException: invalid provider: null
                  at android.location.LocationManager.checkProvider(LocationManager.java:1761)
                  at android.location.LocationManager.requestLocationUpdates(LocationManager.java:464)
                  at com.project.korsa.korsa.YourLocation.onCreate(YourLocation.java:135)
                  at android.app.Activity.performCreate(Activity.java:6904)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3252)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3401) 
                  at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:148) 
                  at android.app.ActivityThread.main(ActivityThread.java:7303) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
azguindou
  • 147
  • 1
  • 3
  • 15
  • Are you sure you explicitly request the location permissions ? Just declaring it in manifest xml won't work as these are dangerous permissions, they need to be taken explicitly. – Kira Jun 25 '18 at 07:34

1 Answers1

0

check this anwser https://stackoverflow.com/a/10688606/3973521

In your call to getBestProvider() you've asked for enabled providers only (that's the second parameter in the method call which you have set to true). If the user has disabled all providers, you will get null.

Community
  • 1
  • 1
Moonlight
  • 232
  • 2
  • 5
  • 18