Android:删除LocationListener
我有以下使用GPS获取用户位置的代码。(代码工作正常)Android:删除LocationListener,android,gps,Android,Gps,我有以下使用GPS获取用户位置的代码。(代码工作正常) 但我的问题是,当我关闭程序(按下后退按钮)时,它会生成错误消息应用程序。。。。。。已停止工作……请重试。 我想我必须删除侦听器,因为我在onDestroy()中使用了removeUpdates()方法,然后也会生成相同的错误消息。 我如何解决它。 我的代码: public class Location_AddressActivity extends Activity { /** Called when the activity i
但我的问题是,当我关闭程序(按下后退按钮)时,它会生成错误消息
应用程序。。。。。。已停止工作……请重试。
我想我必须删除侦听器,因为我在onDestroy()中使用了removeUpdates()方法,然后也会生成相同的错误消息。
我如何解决它。
我的代码:
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");
}
}
}
公共类位置\u AddressActivity扩展活动{
/**在首次创建活动时调用*/
文本视图txt_逻辑;
TextView txt_纬度;
TextView txt_地址;
LocationListener mlocListener;
地点经理;
地理编码器;
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txt_logitude=(TextView)findViewById(R.id.txt_logitude);
txt_纬度=(TextView)findViewById(R.id.txt_纬度);
txt\u地址=(TextView)findViewById(R.id.txt\u地址);
geocoder=新的geocoder(这个,Locale.ENGLISH);
txt_lation.setText(“”);
txt_logitude.setText(“”);
txt_address.setText(“”);
mlocManager=(LocationManager)getSystemService(Context.LOCATION\u服务);
mlocListener=新的MyLocationListener();
mlocManager.RequestLocationUpdate(LocationManager.GPS_提供程序,0,0,mlocListener);
}
公共空间
{
mlocManager.removeUpdates(mlocListener);
}
类MyLocationListener实现LocationListener
{
已更改位置上的公共无效(位置){
双纬度=location.getLatitude();
double longitude=location.getLongitude();
txt_latitude.setText(新的双精度(latitude.toString());
txt_logitude.setText(新的双精度(经度).toString());
试一试{
列表地址=geocoder.getFromLocation(纬度,经度,1);
如果(地址!=null){
返回的地址地址=地址。获取(0);
StringBuilder strReturnedAddress=新StringBuilder(“地址:\n”);
对于(int i=0;i您已在活动的onDestroy()
中删除对super.onDestroy
的调用。只需按以下方式更改该代码,然后重试
public void onDestroy()
{
super.onDestroy();
mlocManager.removeUpdates(mlocListener);
}
我希望removeUpdates()不会给您带来任何问题
location.removeUpdates(your listener);
这是删除location listener的正确方法我想当您通过实现LocationListener类自定义location listener时,您应该创建MyLocationListener的实例,在这里您注册的是超级类,您也在自定义它。这没有意义
请在创建并将该对象用于请求和删除更新时尝试上面的MyLocationListener mLocationListener。请提供崩溃日志。