-1

I am getting an issue with my app on S5 devices (test on two), but it works fine on s4 and s3. I think it has something to do with Kitkat API 19 vs 18. I tested various configurations and while it would work on the S3 every single time, the S5 would not work unless location services were on and I loaded a fresh install. I have put all relevant code below:

@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);

The setup for Location Services

    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) {
    }
};

I get an error telling me there is a NullPointException at the Query (once again only on the s5 device), but I don't understand why it only happens on the API 19 phones. I would appreciate any help.

smusing
  • 15
  • 1
  • 4
  • Latest version of Android as of today (23rd May-2014) is 4.4.2. You probably mistaken on some information there. – Budius May 23 '14 at 19:55
  • Sorry about that I got confused and though API 19 was 4.4.3, I changed the contents to reflect the API levels only. – smusing May 23 '14 at 19:59

1 Answers1

0

The method getLastKnownLocation is not guarantee to return you a value, that way it's understandable that it might crash on the Query line, a common fix would be something like that:

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
}

why this S5 is not giving you a location, that's a different subject. Maybe location services is disabled on the settings?

Budius
  • 39,391
  • 16
  • 102
  • 144