0
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
    }
}

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

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);
    }
}
4

2 回答 2

2

您可以将点声明为活动类的字段,然后您将可以访问这两个类。否则你不能在外部类中访问内部类的字段/变量。您只能访问类的静态字段。

所以将您的代码更改为以下内容:

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
    }
}
于 2012-08-31T12:41:18.530 回答
0

创建内部类的实例时,请保留对它的引用:

MyGestureDetector dectector = new MyGestureDetector();
itemizedOverlay.setGestureDetector(new GestureDetector(detector));

ArrayList<GeoPoint> points = detector.points;
于 2012-08-31T12:41:41.603 回答