Reputation: 1274
I have following code to get User Location using GPS.(Code is working fine)
But My problem is that When I close Program (Press back button) It generates error message
The Application ...... has stopped work .........Please try again. .
I think that I have to remove listener for that I have use removeUpdates() method in onDestroy() then also same error message is generate.
How I can resolve It.
My code :
public class Location_AddressActivity extends Activity {
/** Called when the activity is first created. */
TextView txt_logitude;
TextView txt_latitude;
TextView txt_address;
LocationListener mlocListener;
LocationManager mlocManager;
Geocoder geocoder;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt_logitude=(TextView)findViewById(R.id.txt_logitude);
txt_latitude=(TextView)findViewById(R.id.txt_latitude);
txt_address=(TextView)findViewById(R.id.txt_address);
geocoder = new Geocoder(this, Locale.ENGLISH);
txt_latitude.setText("");
txt_logitude.setText("");
txt_address.setText("");
mlocManager= (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
public void onDestroy()
{
mlocManager.removeUpdates(mlocListener);
}
class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location location) {
double latitude=location.getLatitude();
double longitude=location.getLongitude();
txt_latitude.setText(new Double(latitude).toString());
txt_logitude.setText(new Double(longitude).toString());
try {
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if(addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
txt_address.setText(strReturnedAddress.toString());
}
else{
txt_address.setText("No Address returned!");
}
}
catch (IOException e) {
e.printStackTrace();
txt_address.setText(e.getMessage());
}
}
public void onProviderDisabled(String provider) {
txt_address.setText("GPS Disable");
}
public void onProviderEnabled(String provider) {
txt_address.setText("GPS Enable");
}
}
}
Thanks..
Upvotes: 2
Views: 9703
Reputation: 3443
I guess when you are customizing the Location Listener by implementing the LocationListener class you should create instance of MyLocationListener , here you are registering the Super class and you are also customizing it .that doesn't make sense .
Try MyLocationListener mLocationListener above onCreate and use that object for request and remove update.
Upvotes: -1
Reputation: 3427
location.removeUpdates(your listener);
this is the correct way to remove location listener
Upvotes: 2
Reputation: 5869
You have removed the call to super.onDestroy
in your onDestroy()
of Your Activity. Just change that Code as below and give a try.
public void onDestroy()
{
super.onDestroy();
mlocManager.removeUpdates(mlocListener);
}
I hope removeUpdates() not creating any issues for you.
Upvotes: 16