android - 在 android 的 LocationManager 中显示 java.lang.illegalargumentexception

标签 android locationmanager illegalargumentexception

我有一个应用程序,我在其中的第一页获取当前位置。在印度应用程序运行良好并获取当前位置。当我们在美国运行应用程序时,应用程序在第一页本身崩溃并抛出 IllegalArgumentException。 I am including the exception details here.

最佳答案

手机没有启用任何提供商来检测位置,即它既没有连接到 GPS 也没有连接到 WI-FI,在这种情况下,提供商正在作为 null 传递。这是假设您为 requestLocationUpdates() 函数提供 provider 参数,找到最佳提供者,如果找不到,它将返回 null。

在进一步处理此事之前,我会先检查是否启用了任何提供程序。为避免将 null 作为提供者传递,请在调用位置更新之前检查提供者是否为 null。

if (provider != null) {
      requestLocationUpdates()
} else {
      // alert user asking him to enable one of the providers.
}

这就是您的做法。

HTH.

关于android - 在 android 的 LocationManager 中显示 java.lang.illegalargumentexception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7512661/

相关文章:

android - 缺少对测试类中测试库的引用

android - 添加指示重复类的新依赖项后项目构建失败

c# - 在 XamarinForms 应用程序中使用 Android 定位服务

ios - 获取用户位置警报不会在 iOS 8 中停留在屏幕上

android - 如何计算总行驶距离?

android - YouTube API v3示例IllegalArgumentException,Android Studio

java - Camel : java. lang.IllegalArgumentException:必须指定 defaultEndpoint

android - 在自定义 SurfaceView 中添加自定义 View

android - 创建自定义 android 键盘布局

android - IllegalArgumentException:接收器未注册