我在S5设备上遇到了我的应用程序问题(测试两个),但它在s4和s3上运行正常。我认为它与Kitkat API 19和18有关。我测试了各种配置,虽然它每次都可以在S3上运行,但是除非位置服务打开并且我加载了全新安装,否则S5将无法工作。我已将所有相关代码放在下面:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//set up Factual
FactualRetrievalTask task=new FactualRetrievalTask();
//set up location
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
String provider=locationManager.getBestProvider(newCriteria(), true);
Location l = locationManager.getLastKnownLocation(provider);
locationManager.requestLocationUpdates(provider, 2000, 10,
locationListener);
//ask for all places within the nearest x meters.
Query q=new Query()
.within(new Circle(l.getLatitude(), l.getLongitude(), 5000))
.sortAsc("$distance")
.only("name","price")
.limit(25);
task.execute(q);
位置服务的设置
public Criteria newCriteria(){
Criteria c = new Criteria();
c.setAccuracy(Criteria.ACCURACY_FINE);
c.setPowerRequirement(Criteria.POWER_LOW);
c.setAltitudeRequired(false);
c.setBearingRequired(false);
c.setSpeedRequired(false);
c.setCostAllowed(true);
return c;
}
public void updatedWithNewLocation(Location location) {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location l = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
}
//Location Listener
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updatedWithNewLocation(location);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
我收到错误,告诉我在查询中有一个NullPointException(再次只在s5设备上),但我不明白为什么它只发生在API 19手机上。我将不胜感激任何帮助。
答案 0 :(得分:0)
方法getLastKnownLocation
并不能保证为您返回一个值,这样可以理解它可能会在Query
行崩溃,一个常见的修复就是这样:
Location l = locationManager.getLastKnownLocation(provider);
if(l != null){
Query q=new Query()
.within(new Circle(l.getLatitude(), l.getLongitude(), 5000))
.sortAsc("$distance")
.only("name","price")
.limit(25);
} else{
// wait until you have a location update `onLocationChanged(Location)` to do the query
}
为什么这个S5没有给你一个位置,这是一个不同的主题。 也许在设置上禁用位置服务?