android - startActivity 非法启动类型

标签 android android-intent android-maps

截至今天上午,我已经尝试整合足够的知识来制作一个非常基本的应用程序来演示一个概念。这个想法是显示谷歌地图,用户按下他们想要添加标记的位置,然后弹出一个屏幕,他们可以在其中填写更多信息,然后当有人点击该标记时显示这些信息。

这是我从 Android Studio base 得到的。

 public class MapsActivity extends FragmentActivity implements 

OnMapReadyCallback {

private GoogleMap mMap;

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

private void setMapLongClick(final GoogleMap map) {
    map.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
        @Override
        public void onMapLongClick(LatLng latLng) {
        }
    });
}

/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.z
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Move the camera to Delft
    LatLng delft = new LatLng(52.003569, 4.372987);
    Float zoom = 15f;
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(delft, zoom));
    setMapLongClick(mMap);
}
}

我试过添加这个

    private void setMarkerClick(final GoogleMap map) {
    map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        Intent intent = new Intent(this, MainActivity.class);
        this.startActivity(intent);
    });
}

但是我得到了“类型的非法开始”错误。我这样做完全错了吗?
有没有更简单的方法向标记添加信息?

最佳答案

这就是您应该如何使用它。

map.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {

    @Override
    public boolean onMarkerClick(Marker marker) {
        Intent intent = new Intent(MainActivity.this, MainActivity.class);
        startActivity(intent);
        return true;
    }
});

关于android - startActivity 非法启动类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52026273/

相关文章:

android - (Android Repository Pattern) 使用从 web 删除的项目更新电话数据库数据

android - 有什么方法可以对所有级别的 api 进行安全的相机 Intent 吗?

Android 通过 Intent 启动或显示另一个应用程序

android - 如何在 Android 应用程序中使用 Google Maps API 显示路线?

Android map 缓存画线

android - Firebase Android onAuthStateChanged() 在 signInWithEmailAndPassword() 之后触发两次

android - 如何在屏幕锁定时显示推送通知

android - Fused Location Api setSmallestDisplacement 忽略/不工作

fragment 中的 Android 加载屏幕

android - 如何在给定的纬度和经度上使用标记/叠加项启动 map Intent ?