【发布时间】: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