如何从主 activity 中的不同 class 调用方法?

How to call a method from a different class inside the main activity?

我正在尝试在使用 osmdroid 成功显示的离线地图上放置一个标记。 为了避免弄乱我的代码。我想在另一个 class 文件中定义覆盖标记的方法。任何帮助将不胜感激。

我的主要activity是这样的。

public class MainActivity extends Activity {
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MapView mapView = (MapView) findViewById(R.id.mapview);
    textView = (TextView) findViewById(R.id.tv_location);

    mapView.setTileSource(TileSourceFactory.MAPNIK);
    mapView.setBuiltInZoomControls(true);
    mapView.setMultiTouchControls(true);
    mapView.setClickable(true);

    MapController mapController = mapView.getController();
    mapController.setCenter(new GeoPoint(22.6772796,79.5897374));
    GeoPoint point1 = new GeoPoint(22.6772796,79.5897374);
    mapController.setZoom(10); 

    LocationManager myLocationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
    LocationListenerProxy llp=new LocationListenerProxy(myLocationManager);
    llp.startListening(gpsLocationListener, 1, 1) ;

    Marker mrkr = new Marker();
    mrkr.displayMarker();
}

我的标记 class 是这样的:

public class Marker {


public void displayMarker(){
    ResourceProxyImpl mResourceProxy = new ResourceProxyImpl(getApplicationContext());

    final RelativeLayout rl = new RelativeLayout(this);

    CloudmadeUtil.retrieveCloudmadeKey(getApplicationContext());

    this.mOsmv = new MapView(this, 256);
    rl.addView(this.mOsmv, new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT));

}

}

不需要为此创建不同的class,您需要自己编写一段代码MainActiviy.java

ArrayList<OverlayItem> anotherOverlayItemArray = new ArrayList<OverlayItem>();
    anotherOverlayItemArray.add(new OverlayItem("Bangalore","India", point1));
    OnItemGestureListener<OverlayItem> myOnItemGestureListener = null;
    ItemizedIconOverlay<OverlayItem> anotherItemizedIconOverlay = new ItemizedIconOverlay<OverlayItem>(this, anotherOverlayItemArray, myOnItemGestureListener);

    map.getOverlays().clear();   
    map.getOverlays().add(anotherItemizedIconOverlay);  
    map.invalidate();

在您的 lib 文件夹中使用 osmdroid-android-4.2.jar 并且不要忘记构建路径。 jar 文件包含所有需要的图像。