本文介绍了IllegalArgumentException:提供程序不存在:Maps V1上为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google Maps API V1.我有这个错误:

I'm using Google Maps API V1.I have this error :

     java.lang.IllegalArgumentException: provider doesn't exisit: null

这是我的代码:

    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    String provider = locationManager.getBestProvider(criteria, true);

    if (provider != null)
    {

        startTime = System.currentTimeMillis();

        geoLocTimeOutTask = new GeoLocTimeOutTask();
        geoLocTimeOutTask.execute();

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    }
    else
    {
        geoLocCallbackObj.geoLocationCallback(tagCallback);

    }

我理解该错误,但我的问题是,该设备为什么会向我显示此错误?我如何避免这种情况呢?

I understand the error, bu my question is, whay the device put me this error ? And how can I avoid this please ?

推荐答案

当设备上不存在该网络提供程序时,您正在请求网络提供程序的更新.您可以替换这两行:

You are requesting updates from the network provider when that provider does not exist on the device. You can replace these two lines:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

使用

locationManager.requestLocationUpdates(provider, 0, 0, locationListener);

您已经在努力寻找设备为您的标准提供的最佳提供商,以便于使用.

You've already gone to the effort of finding the best provider the device offers for your criteria so that is the one to use.

或者,在从提供者那里注册更新之前,先检查提供者是否存在:

Alternatively, check the provider exists before registering for updates from it:

if (locationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER))
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

if (locationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER))
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

您还将时间和最小距离都设为零.这非常适合在虚拟设备上进行测试,但请记住在使用真实设备时进行更改,否则会消耗大量电池.

You are also using zero for both time and minimum distance. This is perfect for testing on a virtual device but remember to change them when using a real device, otherwise battery drain will be high.

这篇关于IllegalArgumentException:提供程序不存在:Maps V1上为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 17:59