mMap.setonMarkerDragListener 使我的应用程序崩溃

mMap.setonMarkerDragListener is making my application crash

我正在做一个应用程序,我想从 google 地图中获取经度和纬度。 我将地图添加到我的 activity 并且它可以工作但是当我添加标记拖动侦听器时应用程序崩溃。这是我的代码:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
    Button go;
     GoogleMap mMap;
     ActivityMapsBinding binding;
    Double latitude,longitude;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        binding = ActivityMapsBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

mMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
    @Override
    public void onMarkerDragStart(@NonNull @NotNull Marker marker) {

    }

    @Override
    public void onMarkerDrag(@NonNull @NotNull Marker marker) {

    }

    @Override
    public void onMarkerDragEnd(@NonNull @NotNull Marker marker) {
       latitude =  marker.getPosition().latitude;
        longitude  =  marker.getPosition().longitude;
    }
});

        // 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);

        go = findViewById(R.id.button1);
        go.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                send_to_act2();
            }
        });
    }

    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * 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;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney").draggable(true));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }

我尝试评论标记拖动并且应用程序运行正常

移动这条语句:

mMap.setOnMarkerDragListener(...
mMap初始化后

到你的onMapReady里面,比如:

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;


    mMap.setOnMarkerDragListener(new GoogleMap.OnMarkerDragListener() {
        @Override
        public void onMarkerDragStart(@NonNull @NotNull Marker marker) {
        }

        @Override
        public void onMarkerDrag(@NonNull @NotNull Marker marker) {
        }

        @Override
        public void onMarkerDragEnd(@NonNull @NotNull Marker marker) {
            latitude =  marker.getPosition().latitude;
            longitude  =  marker.getPosition().longitude;
        }
    });

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney").draggable(true));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

您不能使用 mMap,直到它被初始化并且在 onMapReady 中异步完成。