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

Posted

技术标签:

【中文标题】mmap.setonMarkerDragListener 使我的应用程序崩溃【英文标题】:mMap.setonMarkerDragListener is making my application crash 【发布时间】:2021-08-28 15:10:16 【问题描述】:

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

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

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

【问题讨论】:

【参考方案1】:

移动此语句:

mMap.setOnMarkerDragListener(...

到你的onMapReady里面mMap被初始化后,如:

@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 中异步完成。

【讨论】:

以上是关于mmap.setonMarkerDragListener 使我的应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章