Question

I'm trying to make a weather app that uses Geocoder to get the current city, and then uses AsyncTask to get weather data from an api and then parse it. But when I input the current city into the AsyncTask execute() method, my app crashes:

private double latitude;
private double longitude;   
String address;
LocationManager lm;
LocationListener ll;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    address = getAddress(latitude, longitude); // getAddress() is below

    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    ll = new myLocationListener();
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}

private class myLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(android.location.Location location) {
        latitude = location.getLatitude();
        longitude = location.getLongitude();
        String address = getAddress(latitude, longitude);
        Task task = new Task(); // Task extends AsyncTask
        task.execute(new String[] { address }); 
    }
    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

public String getAddress(double lat, double lon) {
    Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
    String ret = "";
    try {
        List<Address> addresses = geocoder.getFromLocation(
                lat, lon, 10);
        if (addresses != null) {
            Address returnedAddress = addresses.get(0);
            StringBuffer str = new StringBuffer();
            str.append(returnedAddress.getLocality());
            str.append(",");
            str.append(returnedAddress.getCountryName());
            ret = str.toString();
        } else {
            ret = "No Address returned!";
        }
    } catch (IOException e) { // TODO Auto-generated catch block
        e.printStackTrace();
        ret = "Can't get Address!";
    }
    return ret;
}

I know for a fact that my getAddress() method works like it's supposed to. When I hardcode a city and state into the address field, it works like a charm. However, when I call my getAddress() method and set it to address, and then input it into execute(), the app crashes and it says that getFromLocation() returned null. Why is this happening? Is it the order of my code? Thank you for all answers in advance.

Was it helpful?

Solution

Make sure your GPS is enabled and you are not testing from the emulator. The provider will return null if it is not enabled as per the documentation. Since you need the current location and probably you do not want updates I will recommend you use requestSingleUpdate(). A better option will be to use the new LocationClient Class that is part of Google Services; its fused provider gets the best Location available for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top