2

我有以下代码使用 GPS 获取用户位置。(代码工作正常)
但我的问题是当我关闭程序时(按返回按​​钮)它会生成错误消息
应用程序......已停止工作...... .....请再试一次。 .
我认为我必须删除侦听器,因为我在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");      
        }           
    }   
}

谢谢..

4

3 回答 3

15

super.onDestroy您已在“您onDestroy()的活动”中删除了对的调用。只需如下更改该代码并尝试一下。

public  void onDestroy()
    {
        super.onDestroy();
        mlocManager.removeUpdates(mlocListener);
    }

我希望 removeUpdates() 不会给您带来任何问题。

于 2013-01-11T09:29:37.327 回答
2
location.removeUpdates(your listener);

这是删除位置监听器的正确方法

于 2013-01-11T09:33:49.003 回答
-1

我想当您通过实现 LocationListener 类来自定义位置侦听器时,您应该创建 MyLocationListener 的实例,在这里您正在注册 Super 类并且您也在自定义它。这没有意义。

尝试 onCreate 上方的 MyLocationListener mLocationListener 并将该对象用于请求和删除更新。

于 2013-01-11T09:44:49.317 回答