0

I am having trouble figuring out why my app force closes when GPS is not enabled. When enabled before running, all is well. Everything worked fine until I added the LocationListener to update current location. I'm sure it is something ridiculously simple.

 public class GlenrochieMain extends Activity

 {


   @Override

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

    //GPS Functionality

    LocationManager locationManager;
    String context = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(context);

    //Criteria for GPS
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    String provider = locationManager.getBestProvider(criteria, true);      

    Location location = locationManager.getLastKnownLocation(provider);
    updateWithNewLocation(location);

    //updates location every 5second+5meters
    locationManager.requestLocationUpdates(provider, 5000, 5,
            locationListener);
  }

private final LocationListener locationListener = new LocationListener() {
    public void onLocationChanged(Location location) {
      updateWithNewLocation(location);
    }

    public void onProviderDisabled(String provider){
      updateWithNewLocation(null);
    }

    public void onProviderEnabled(String provider){ }
    public void onStatusChanged(String provider, int status, 
                                Bundle extras){ }
  };

  private void updateWithNewLocation(Location location) {
    String latLongString;
    TextView myLocationText; 
    myLocationText = (TextView)findViewById(R.id.myLocationText);
    if (location != null) {
      double lat = location.getLatitude();
      double lng = location.getLongitude();
      latLongString = "Lat:" + lat + "\nLong:" + lng;
    } else {
      latLongString = "No location found"; 
    }
    myLocationText.setText("Your Current Location is:\n" + 
                           latLongString);
  }   
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
Kal
  • 1
  • 1

2 Answers2

1

You need to make sure that the GPS is actually enabled. This answer is for Android 1.6, see How can I check the current status of the GPS receiver? for later versions.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
0
String provider = locationManager.getBestProvider(criteria, true);  

When no suitable provider is found, getBestProvider will return null. You have not handled that case.

Sarwar Erfan
  • 18,034
  • 5
  • 46
  • 57
  • Could you explain what I need to change to fix this problem? – Kal Mar 09 '11 at 19:22
  • after this statement, check whether `provider` is null. If so, ask user to enable gps/open gps enabling dialog. If this is null, you cannot call `getLastKnownLocation` – Sarwar Erfan Mar 10 '11 at 03:22