如何访问';是在java android中扩展simpleOnGestureListener的内部类中吗?

如何访问';是在java android中扩展simpleOnGestureListener的内部类中吗?,java,android,Java,Android,这是内部类,问题是我想访问MyActivity类中的点的ArrayList,但似乎我必须使所有变量都是静态的,这最终会给我带来错误。如有任何变通或解决方案,将不胜感激 public class MyActivity extends MapActivity implements LocationListener, OnClickListener { private MapView mapView; private MyItemizedOverlay itemizedOverlay

这是内部类,问题是我想访问MyActivity类中的点的ArrayList,但似乎我必须使所有变量都是静态的,这最终会给我带来错误。如有任何变通或解决方案,将不胜感激

public class MyActivity extends MapActivity implements LocationListener, OnClickListener {

    private MapView mapView;
    private MyItemizedOverlay itemizedOverlay;
    Button route;
    boolean shadow;
    private LocationManager locManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        route = (Button) findViewById(R.id.cmd_submit);
        route.setOnClickListener(this);



        //fetch the map view from the layout
        mapView = (MapView) findViewById(R.id.myMapView);

        //make available zoom controls
        mapView.setBuiltInZoomControls(true);



        //latitude and longitude of Rome
        double lat = 41.889882;
        double lon = 12.479267;

        //create geo point
        GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));

        //get the MapController object
        MapController controller = mapView.getController();

        //animate to the desired point
        controller.animateTo(point);

        //set the map zoom to 13
        // zoom 1 is top world view
        controller.setZoom(13);

        //invalidate the map in order to show changes
        mapView.invalidate();



        // Use the location manager through GPS
        locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, this);

        //get the current location (last known location) from the location manager
        Location location = locManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);


        //if location found display as a toast the current latitude and longitude
        if (location != null) {

            Toast.makeText(
                    this,
                    "Current location:\nLatitude: " + location.getLatitude()
                    + "\n" + "Longitude: " + location.getLongitude(),
                    Toast.LENGTH_LONG).show();

            point = new GeoPoint((int) (location.getLatitude() * 1E6), (int) (location.getLongitude()
                    * 1E6));

            controller.animateTo(point);


        } else {

            Toast.makeText(this, "Cannot fetch current location!",
                    Toast.LENGTH_LONG).show();
        }

        //when the current location is found – stop listening for updates (preserves battery)
        locManager.removeUpdates(this);

        // fetch the drawable - the pin that will be displayed on the map
        Drawable drawable = this.getResources().getDrawable(R.drawable.marker);

        // create and add an OverlayItem to the MyItemizedOverlay list
        OverlayItem overlayItem = new OverlayItem(point, "", "");

        itemizedOverlay = new MyItemizedOverlay(drawable, this);

        itemizedOverlay.setGestureDetector(new GestureDetector(new MyGestureDetector()));

        itemizedOverlay.addOverlay(overlayItem);


        // add the overlays to the map
        mapView.getOverlays().add(itemizedOverlay);
        mapView.invalidate();

        //when the current location is found – stop listening for updates (preserves battery)
        locManager.removeUpdates(this);
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;

    }

    /* When the activity starts up, request updates */
    @Override
    protected void onResume() {
        super.onResume();
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        locManager.removeUpdates(this); //activity pauses => stop listening for updates
    }
}
公共类MyGestureDetector扩展了SimpleGestureListener{
ArrayList points=新的ArrayList();
@凌驾
公共布尔值OnSingleTapConfiged(MotionEvent事件){
//从地图上取对应点
Log.d(“A条件”,“d5al l方法A”);
地理点p=mapView.getProjection().fromPixels((int)event.getX(),(int)event.getY());
增加(p);
//创建覆盖项并清除所有其他项
OverlayItem o=新的OverlayItem(p,null,null);
项目化覆盖。添加覆盖(o);
如果(points.size()>1){
Log.d(“点为“,”+点);
}
//添加覆盖项
//mapView.getOverlays().clear();
mapView.getOverlays().add(ItemizeOverlay);
mapView.invalidate();
Geocoder Geocoder=新的地理编码器(getBaseContext(),
Locale.getDefault());
//根据坐标获取地址
试一试{
列表地址=geoCoder.getFromLocation(p.getLatitudeE6()/1E6,p.getLongitudeE6())
/1E6,1);
字符串地址字符串=”;
如果(地址.size()>0){
for(int i=0;i
您可以将点声明为活动类的字段,然后您就可以访问这两个类。 否则您无法在外部类中访问内部类的字段/变量。只能访问类的静态字段

因此,请将代码更改为以下内容:

public class MyGestureDetector extends SimpleOnGestureListener {

    ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();

    @Override
    public boolean onSingleTapConfirmed(MotionEvent event) {

        // fetch the correspondent point from the map

        Log.d("A condition", "d5al l methodaaya");
        GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(), (int) event.getY());
        points.add(p);




        // create an overlay item and clear all others
        OverlayItem o = new OverlayItem(p, null, null);
        itemizedOverlay.addOverlay(o);

        if (points.size() > 1) {
            Log.d("Points are", "" + points);

        }

        // add the overlay item
        //mapView.getOverlays().clear();
        mapView.getOverlays().add(itemizedOverlay);
        mapView.invalidate();

        Geocoder geoCoder = new Geocoder(getBaseContext(),
                Locale.getDefault());

        // get the address based on the coordinates
        try {
            List<Address> addresses = geoCoder.getFromLocation(p.getLatitudeE6() / 1E6, p.getLongitudeE6()
                    / 1E6, 1);

            String addressString = "";
            if (addresses.size() > 0) {
                for (int i = 0; i < addresses.get(0)
                        .getMaxAddressLineIndex(); i++)
                    addressString += addresses.get(0).getAddressLine(i)
                            + " - ";
            }

            Toast.makeText(getBaseContext(), addressString,
                    Toast.LENGTH_SHORT).show();








        } catch (IOException e) {
            e.printStackTrace();
        }

        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        return super.onFling(e1, e2, velocityX, velocityY);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }
}

public void onClick(View arg0) {

    //System.out.println(points);
    if (route == arg0) {
        Intent i = new Intent(this, ChoosingClues.class);
        startActivity(i);
    }
}
公共类MyActivity扩展MapActivity实现LocationListener、OnClickListener{
私有地图视图;
私有MyItemizedOverlay itemizedOverlay;
按钮路线;
布尔阴影;
私人场所经理;
ArrayList points=新的ArrayList();
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
路由=(按钮)findViewById(R.id.cmd_submit);
route.setOnClickListener(this);
//从布局中获取地图视图
mapView=(mapView)findViewById(R.id.myMapView);
//使缩放控件可用
mapView.SetBuilTinZoomControl(真);
//罗马经纬度
双lat=41.889882;
双lon=12.479267;
//创建地理点
地质点=新的地质点((int)(纬度*1E6),(int)(纬度*1E6));
//获取MapController对象
MapController=mapView.getController();
//设置动画到所需的点
控制器。动画(点);
//将地图缩放设置为13
//zoom 1是顶级世界视图
控制器。设置缩放(13);
//使映射无效以显示更改
mapView.invalidate();
//通过GPS使用位置管理器
locManager=(LocationManager)getSystemService(Context.LOCATION\u服务);
LocationManager.RequestLocationUpdate(LocationManager.GPS_提供程序,0,
0,这个);
//从位置管理器获取当前位置(最后一个已知位置)
位置=本地管理器
.getLastKnownLocation(LocationManager.GPS\U提供商);
//如果找到位置,则显示为当前纬度和经度
如果(位置!=null){
Toast.makeText(
这
当前位置:\nLatitude:+location.getLatitude()
+“\n”+“经度:”+location.getLongitude(),
Toast.LENGTH_LONG).show();
点=新的地质点((int)(location.getLatitude()*1E6),(int)(location.getLatitude())
*1E6);
控制器。动画(点);
}否则{
Toast.makeText(此“无法获取当前位置!”,
Toast.LENGTH_LONG).show();
}
//找到当前位置后–停止侦听更新(保留电池)
locManager.RemoveUpdate(此);
//获取可绘制的-将显示在地图上的pin
Drawable Drawable=this.getResources().getDrawable(R.Drawable.marker);
//创建覆盖项并将其添加到MyItemizedOverlay列表中
OverlayItem OverlayItem=新的OverlayItem(点“,”);
itemizedOverlay=新的MyItemizedOverlay(可绘制,此);
itemizedOverlay.setGestureDetector(新的GestureDetector(新的MyGestureDetector());
itemizedOverlay.addOverlay(overlayItem);
//将覆盖图添加到地图中
mapView.getOverlays().add(ItemizeOverlay);
mapView.invalidate();
//找到当前位置后–停止侦听更新(保留电池)
瞧
public class MyActivity extends MapActivity implements LocationListener, OnClickListener {

    private MapView mapView;
    private MyItemizedOverlay itemizedOverlay;
    Button route;
    boolean shadow;
    private LocationManager locManager;
    ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        route = (Button) findViewById(R.id.cmd_submit);
        route.setOnClickListener(this);



        //fetch the map view from the layout
        mapView = (MapView) findViewById(R.id.myMapView);

        //make available zoom controls
        mapView.setBuiltInZoomControls(true);



        //latitude and longitude of Rome
        double lat = 41.889882;
        double lon = 12.479267;

        //create geo point
        GeoPoint point = new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));

        //get the MapController object
        MapController controller = mapView.getController();

        //animate to the desired point
        controller.animateTo(point);

        //set the map zoom to 13
        // zoom 1 is top world view
        controller.setZoom(13);

        //invalidate the map in order to show changes
        mapView.invalidate();



        // Use the location manager through GPS
        locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, this);

        //get the current location (last known location) from the location manager
        Location location = locManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);


        //if location found display as a toast the current latitude and longitude
        if (location != null) {

            Toast.makeText(
                    this,
                    "Current location:\nLatitude: " + location.getLatitude()
                    + "\n" + "Longitude: " + location.getLongitude(),
                    Toast.LENGTH_LONG).show();

            point = new GeoPoint((int) (location.getLatitude() * 1E6), (int) (location.getLongitude()
                    * 1E6));

            controller.animateTo(point);


        } else {

            Toast.makeText(this, "Cannot fetch current location!",
                    Toast.LENGTH_LONG).show();
        }

        //when the current location is found – stop listening for updates (preserves battery)
        locManager.removeUpdates(this);

        // fetch the drawable - the pin that will be displayed on the map
        Drawable drawable = this.getResources().getDrawable(R.drawable.marker);

        // create and add an OverlayItem to the MyItemizedOverlay list
        OverlayItem overlayItem = new OverlayItem(point, "", "");

        itemizedOverlay = new MyItemizedOverlay(drawable, this);

        itemizedOverlay.setGestureDetector(new GestureDetector(new MyGestureDetector()));

        itemizedOverlay.addOverlay(overlayItem);


        // add the overlays to the map
        mapView.getOverlays().add(itemizedOverlay);
        mapView.invalidate();

        //when the current location is found – stop listening for updates (preserves battery)
        locManager.removeUpdates(this);
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;

    }

    /* When the activity starts up, request updates */
    @Override
    protected void onResume() {
        super.onResume();
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        locManager.removeUpdates(this); //activity pauses => stop listening for updates
    }
}
MyGestureDetector dectector = new MyGestureDetector();
itemizedOverlay.setGestureDetector(new GestureDetector(detector));

ArrayList<GeoPoint> points = detector.points;