通过单击“信息窗口”打开活动

时间:2019-06-15 22:56:20

标签: java android google-maps google-maps-api-3 google-maps-api-2

我正在尝试显示来自MapActivity的新活动。 实际上,我想通过单击地图上“标记”的InfoWindow来打开一个新活动

我试图在OnInfoWindowClick方法中使用意图,但是当我单击InfoWindow时,应用程序仍然崩溃

这是我的MapActivity:

public class MapsActivity extends FragmentActivity implements GoogleMap.OnMarkerClickListener, OnMapReadyCallback, GoogleMap.OnMapClickListener {

private GoogleMap mMap;
private Marker mMarker;
int i = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.setMapType(mMap.MAP_TYPE_NORMAL); // Here is where you set the map type
    // Add a marker in Sydney and move the camera
    LatLng dfltMarkLyon = new LatLng(45.760102,4.839177);
    mMarker.setTag(0);
    mMarker = mMap.addMarker(new MarkerOptions().position(new LatLng(45.756363,4.833219)).title("L'Institut Restaurant").snippet("Restaurant").icon(BitmapDescriptorFactory
            .defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
    mMarker.setTag(1);

    mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {
            Toast.makeText(MapsActivity.this, "Test " + i, Toast.LENGTH_SHORT).show();
            i += 1;
            Intent intent = new Intent(MapsActivity.this, PlaceActivity.class);
            startActivity(intent);
        }
    });

    //mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

    float zoomLevel = 13.0f; //This goes up to 21
    mMap.setOnMapClickListener(this);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(dfltMarkLyon, zoomLevel));
}

这是我要打开的活动所在的课程:

   public class PlaceActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_description);
        //Toast.makeText(PlaceActivity.this, "New Class", Toast.LENGTH_SHORT).show();setContentView(R.layout.activity_description);

    }


    }

当我点击InfoWindow时,我的应用程序崩溃了

该怎么做才能改善呢?

1 个答案:

答案 0 :(得分:1)

您的代码(正在启动新活动)是正确的。 您可以同时使用。

Intent intent = new Intent(MapsActivity.this, PlaceActivity.class);
Intent intent = new Intent(getContext(), PlaceActivity.class);

问题似乎出在PlaceActivity中。 请检查您是否将此活动添加到AndroidManifest.xml文件中。 并检查资源中是否有activity_description.xml个文件。

问题取决于您的项目设置和结构。 您可以通过在PlaceActivity中将开始活动更改为AndroidManifest.xml进行检查。

相关问题