locationManager.getBestProvider 返回 null

Posted

技术标签:

【中文标题】locationManager.getBestProvider 返回 null【英文标题】:locationManager.getBestProvider return null 【发布时间】:2017-09-24 09:50:25 【问题描述】:

显然,我的提供商有问题,尽管我在清单中启用了 COARSE 和 FINE 位置权限,但我的应用程序在启动时崩溃。我还添加了 INTERNET 权限。

我的代码:

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);

这里是 logcat:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.project.korsa.korsa, PID: 32462
              java.lang.RuntimeException: Unable to start activity ComponentInfocom.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) 

【问题讨论】:

您确定明确请求位置权限吗?仅在清单 xml 中声明它是行不通的,因为这些是危险的权限,需要明确地采用。 【参考方案1】:

检查这个答案 https://***.com/a/10688606/3973521

在调用 getBestProvider() 时,您已要求启用提供程序 仅(这是您拥有的方法调用中的第二个参数 设置为真)。如果用户禁用了所有提供程序,您将获得 空。

【讨论】:

以上是关于locationManager.getBestProvider 返回 null的主要内容,如果未能解决你的问题,请参考以下文章