[英]Open Activity by clicking on InfoWindow
我正在嘗試顯示來自MapActivity
的新活動。 事實上,我想通過單擊我的地圖上的標記的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));
}
這是我要打開的活動所在的班級:
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
我的應用程序崩潰了
我能做些什么來改善它?
您開始新活動的代碼是正確的。 您可以同時使用兩者。
Intent intent = new Intent(MapsActivity.this, PlaceActivity.class);
Intent intent = new Intent(getContext(), PlaceActivity.class);
問題似乎出在PlaceActivity
。 請檢查您是否在AndroidManifest.xml
文件中添加了此活動。 並檢查資源中是否有activity_description.xml
文件。
這是取決於您的項目設置和結構的問題。 您可以通過在AndroidManifest.xml
中將開始活動更改為PlaceActivity
來檢查它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.