LocationManager的getBestProvider返回null

时间:2012-05-21 14:41:37

标签: android android-location

我有一群人报告了一个我无法重现的错误。在尝试打开MapView时,报告getBestProvider返回null并且我知道这意味着找不到符合ACCURACY_COARSE条件的提供商,因此如果没有GPS然后它应该回到network or passive,但显然不会在这些用户设备上。那甚至意味着GPS关闭并且没有网络连接?

我尝试关闭GPS并将手机置于飞行模式以尝试重现问题,但我仍然能够在没有强制关闭的情况下打开mapview,所以我不知道如何处理这个问题。

locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
geocoder = new Geocoder(this);

Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
List<String> lProviders = locationManager.getProviders(false);
for(int i=0; i<lProviders.size(); i++){
    Log.d("LocationActivity", lProviders.get(i));
}
String provider = locationManager.getBestProvider(criteria, true); // null

long minTime = 60000;
float minDistance = 5;

locationManager.requestLocationUpdates(provider, minTime, minDistance, this);

错误

java.lang.RuntimeException: Unable to start activity ComponentInfo{ecm2.android/ecm2.android.LocationActivity}: 
java.lang.IllegalArgumentException: provider==null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: provider==null
at android.location.LocationManager.requestLocationUpdates(LocationManager.java:625)
at ecm2.android.LocationActivity.onCreate(LocationActivity.java:142)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

2 个答案:

答案 0 :(得分:22)

在致电getBestProvider()时,您已经要求启用的提供商(这是您已设置为true的方法调用中的第二个参数)。如果用户已停用所有提供商,您将获得null

在任何情况下,您都需要能够处理没有可用提供商的情况,因此您需要检查null并告诉用户他没有启用任何位置。< / p>

答案 1 :(得分:6)

<强>的AndroidManifest.xml

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

<强> MapsActivity.java

 @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (permissions.length == 1 &&
            permissions[0] == android.Manifest.permission.ACCESS_FINE_LOCATION &&
            grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        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) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        mMap.setMyLocationEnabled(true);
    } else {
        // Permission was denied. Display an error message.
    }
}

<强> MapsActivity.java/onCreate()

if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        mMap.setMyLocationEnabled(true);
    } else {
        ActivityCompat.requestPermissions(this,new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
    }

这项工作对我而言;