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

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

问题描述

我有一堆人报告说,我无法重现错误。当试图打开一个图形页面这是报告的 getBestProvider 被返回null,我知道这意味着,没有提供者发现符合我的标准是 ACCURACY_COARSE 因此,如果没有GPS,那么它应该继续采用网​​络或被动但它显然不会对这些用户设备。什么,即使意思呢,全球定位系统处于关闭状态,没有网络连接?

我试图转弯GPS关闭,并把手机进入飞行模式,试图重现该问题但我仍然能够打开的MapView没有强制关闭,所以我不知道如何处理这个问题。

locationManager =(LocationManager)this.getSystemService(LOCATION_SERVICE); 地理codeR =新的地缘codeR(本); 标准标准=新标准(); criteria.setAccuracy(Criteria.ACCURACY_COARSE); 名单<字符串> lProviders = locationManager.getProviders(假); 的for(int i = 0; I< lProviders.size();我++){     Log.d(LocationActivity,lProviders.get(ⅰ)); } 字符串商= locationManager.getBestProvider(标准,真正的); // 空值 长minTime = 60000; 浮minDistance依旧= 5; locationManager.requestLocationUpdates(供应商,minTime,minDistance依旧,这一点);

错误

 了java.lang.RuntimeException:无法启动活动ComponentInfo {ecm2.android/ecm2.android.LocationActivity}:
java.lang.IllegalArgumentException:如果供应商== NULL
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)
在android.os.Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:123)
在android.app.ActivityThread.main(ActivityThread.java:4627)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:521)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:860)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
在dalvik.system.NativeStart.main(本机方法)
致:java.lang.IllegalArgumentException:如果供应商== NULL
在android.location.LocationManager.requestLocationUpdates(LocationManager.java:625)
在ecm2.android.LocationActivity.onCreate(LocationActivity.java:142)
在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
 

解决方案

在您的来电 getBestProvider()你问启用仅(这是在方法调用您已设置为真正的第二个参数)。如果用户禁用了所有的供应商,您将获得

在任何情况下,你需要能够应付那里有没有可用的供应商的情况,所以你需要检查,告诉用户他没有启用任何位置。

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?

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

error

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)

解决方案

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.

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天全站免登陆