我在我的一个应用程序中遇到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);
}
答案 0 :(得分:0)
如果没有对代码进行过多分析,是否会出现权限问题?也许您的应用不允许使用GPS数据,这会导致意外错误?
-Woody
答案 1 :(得分:0)
我没看到您注册位置更新的位置?也许这就是你在这里所缺少的。
答案 2 :(得分:0)
我在发布之前不小心从我的代码中删除了一行,因为我原以为它只是一行未使用的代码。然而,它引起了这个问题。在以下行中,如果用户禁用了NETWORK_PROVIDER,则baseLocation将等于null。
long fixTime = baseLocation.getTime();
修复:不要尝试在Null位置对象上调用方法getTime():)谢谢大家!