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.onDestroyonDestroy()。そのコードを以下のように変更して試してみてください。

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 クラスを実装して Location Listener をカスタマイズするときは、 MyLocationListener のインスタンスを作成する必要があると思います。ここでは、Super クラスを登録していて、それもカスタマイズしています。それは意味がありません。

onCreate の上で MyLocationListener mLocationListener を試し、そのオブジェクトを使用して更新を要求および削除します。

于 2013-01-11T09:44:49.317 に答える