LocationManager 的 getBestProvider 返回 null [英] LocationManager's getBestProvider returning null

查看:31
本文介绍了LocationManager 的 getBestProvider 返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多人报告了我无法重现的错误.尝试打开 MapView 时,它报告 getBestProvider 返回 null,我知道这意味着找不到满足我的标准 ACCURACY_COARSE<的提供程序/code> 所以如果没有 GPS 那么它应该依靠 network 或passive 但它显然不在那些用户设备上.如果 GPS 关闭且没有网络连接,这意味着什么?

I have a bunch of people reporting an error that I cannot reproduce. When trying to open a MapView it's reporting that the getBestProvider is returning null and I know that means that that no provider is found that fulfills my criteria which is ACCURACY_COARSE so if there is no GPS then it should fall back on either network or passive but it obviously doesn't on those user devices. What would that even mean then that GPS is off and there is no network connection?

我尝试关闭 GPS 并将手机置于飞行模式以尝试重现该问题,但我仍然能够在不强制关闭的情况下打开地图视图,因此我不知道如何处理此问题.

I tried turning GPS off and putting the phone into airplane mode to try to reproduce the problem but still I was able to open the mapview without a force close so I don't know how to handle this problem.

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)

推荐答案

在您对 getBestProvider() 的调用中,您只要求启用的提供程序(这是第二个您已设置为 true 的方法调用中的参数).如果用户禁用了所有提供程序,您将获得 null.

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.

无论如何,您需要能够处理没有可用提供程序的情况,因此您需要检查 null 并告诉用户他没有启用任何位置.

In any case, you need to be able to deal with the situation where there are no available providers, so you'll need to check for null and tell the user that he has not enabled any location.

这篇关于LocationManager 的 getBestProvider 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆