单击信息窗口打开 Activity

Open Activity by clicking on InfoWindow

我正在尝试显示来自 MapActivity 的新 activity。 事实上,我想打开一个新的 activity,方法是点击地图

上标记的 InfoWindow

我尝试在 OnInfoWindowClick 方法中使用 intent,但当我单击 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));
}

这是 class 我想打开 activity 所在的位置 :

   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 我的应用程序崩溃

我可以做些什么来改善它?

您的代码是正确的 activity。 您可以同时使用两者。

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

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

这个问题取决于你的项目设置和结构。 您可以通过将开始 activity 更改为 PlaceActivity in AndroidManifest.xml.

来检查它