gpt4 book ai didi

java - 如何访问 java android 中扩展 simpleOnGestureListener 的内部类内部的变量?

转载 作者:行者123 更新时间:2023-12-02 07:36:01 25 4
gpt4 key购买 nike

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

最佳答案

您可以将点声明为 Activity 类的字段,然后您就可以在这两个类中访问。否则您无法访问外部类中内部类的字段/变量。您只能访问该类的静态字段。

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

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

关于java - 如何访问 java android 中扩展 simpleOnGestureListener 的内部类内部的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12215549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com