【问题标题】:Android Mystery NullPointerException getting last known locationAndroid Mystery NullPointerException 获取最后一个已知位置
【发布时间】:2011-08-09 01:04:56
【问题描述】:

我的一个应用程序中出现 NullPointerException。我曾以为我在这段代码中为 Null 引用考虑了所有地方,但似乎我可能遗漏了一些东西。我仔细查看了代码,但我仍然不确定这个 NullPointerException 是从哪里抛出的。

这是用户报告的错误,所以我无法调试,我在开发过程中也没有遇到过这个错误

代码/应用程序中发生了什么:

用户启动应用程序,我尝试通过 NETWORK 或 GPS 获取他们最后的已知位置。一旦我设置了位置(如果可用),我稍后会获取该位置的天气并将其显示给用户。

有没有人知道这段代码在哪里可能会失败?我应该做一些关于 GPS/NETWORK 的进一步检查吗?非常感谢任何帮助!

堆栈跟踪

java.lang.NullPointerException
at com.hookedroid.fishingcompanion.BaseActivity.getLastKnownLocation(BaseActivity.java:337)
at com.hookedroid.fishingcompanion.BaseActivity.getWeatherbyCurrent(BaseActivity.java:164)
at com.hookedroid.fishingcompanion.BaseActivity.getWeather(BaseActivity.java:138)
at com.hookedroid.fishingcompanion.MainFish.syncWeather(MainFish.java:214)
at com.hookedroid.fishingcompanion.MainFish.run(MainFish.java:206)
at java.lang.Thread.run(Thread.java:1019)

有问题的代码

// Gets the user's current NETWORK/GPS location
// then gets weather for that location
private void getWeatherbyCurrent() throws WeatherException {
    Criteria criteria = new Criteria();
    locationProvider = locManager.getBestProvider(criteria, true);

    // Error is thrown on this method call
    getLastKnownLocation();

    getLocationBasedWeather();
}

public void getLastKnownLocation() throws WeatherException {
    String provider;
    //Check if NETWORK Provider is enabled.  If enabled, get it's last known location.
    //If location is received, set baseLocation and return back to get weather.
    //If location is not received, continue on and try to get location from GPS Provider.
    if (isNetworkProviderEnabled()) {
        provider = LocationManager.NETWORK_PROVIDER;
        baseLocation = locManager.getLastKnownLocation(provider);
        // This is Line 337.  I had removed it from the posted code as it was unused
        // and didn't think it was causing an issue.  
        long fixTime = baseLocation.getTime();

        //Location returned from NETWORK_PROVIDER, return/stop executing code and get weather for this location
        if (baseLocation != null)
            return;
    }
    if (baseLocation == null && isGPSEnabled()) {
        provider = LocationManager.GPS_PROVIDER;
        baseLocation = locManager.getLastKnownLocation(provider);
    }
    else {
        if (locationProvider == null) {
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            locationProvider = locManager.getBestProvider(criteria, true);
        }

        if (locationProvider != null)
            baseLocation = locManager.getLastKnownLocation(locationProvider);
        else {
            if (!isGPSEnabled && !isNetworkProviderEnabled)
                throw new WeatherException("No Location Providers are available.");
        }
    }
}
//Returns whether or not the NETWORK_PROVIDER is enabled
public boolean isNetworkProviderEnabled() {
    checkNetworkProvider();
    return this.isNetworkProviderEnabled;
}

//Check if the NETWORK_PROVIDER is enabled
public void checkNetworkProvider() {
    isNetworkProviderEnabled = locManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
//Returns whether or not the GPS_PROVIDER is enabled
public boolean isGPSEnabled() {
    checkGPS();
    return this.isGPSEnabled;
}
//Check if the GPS_PROVIDER is enabled
public void checkGPS() {
    isGPSEnabled = locManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}

【问题讨论】:

  • 你能不能放一些带有重要行号的cmets?
  • 第 337 行是哪一行?
  • Pravin:上面的代码中实际上缺少第 337 行,我已将其删除,因为它看起来像是我在启动之前忘记删除的未使用的代码。但是,第 337 行没有重新插入到上面的“有问题的代码”部分。

标签: android nullpointerexception


【解决方案1】:

如果不对您的代码进行过多分析,会不会是权限问题?也许您的应用不允许使用 GPS 数据,导致意外错误?

-伍迪

【讨论】:

  • 我已经设置了所有正确的权限,这是我唯一一次看到数百名用户报告此错误
  • 上面提到的PravinCG,能指出哪一行是337吗?
【解决方案2】:

我没有看到您在哪里注册位置更新?也许这就是你在这里所缺少的。

【讨论】:

    【解决方案3】:

    我在发布之前不小心从代码中删除了一行,因为我认为这只是一行未使用的代码。然而,它导致了问题。在以下行中,如果用户禁用了 NETWORK_PROVIDER,则 baseLocation 将等于 null。

    long fixTime = baseLocation.getTime();
    

    修复:不要尝试在 Null 位置对象上调用方法 getTime() :) 谢谢大家!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-08
      • 2014-10-18
      • 2013-03-29
      相关资源
      最近更新 更多