添加Onmarkerclicklistener后崩溃了

时间:2016-04-22 19:21:53

标签: android google-maps google-maps-markers onclicklistener

与标题一样,如果我添加OnMarkerClickListener或OnInfoWindowClickListener,我的应用程序会崩溃。请帮我。我已经更新了代码 这是我的代码,对不起,我是新手:|

@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(MapsActivity.this);
        btnBeHost = (Button)findViewById(R.id.btnBeHost);
        requestQueue  = Volley.newRequestQueue(this);

        //be a host btn when clicked
        ...

        //Welcome
        ...

        //get coordinates of motels and show on map
        //latitude request
        ...
        //longitude request
        ...
        RequestQueue queue = Volley.newRequestQueue(MapsActivity.this);
        queue.add(latReq);
        queue.add(lngReq);

        mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                Toast.makeText(MapsActivity.this,"zzzz",Toast.LENGTH_LONG).show();
                return false;
            }
        });
    }
    @TargetApi(Build.VERSION_CODES.M)
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.getUiSettings().setZoomControlsEnabled(true);
        //set my location enabled

...         }

1 个答案:

答案 0 :(得分:1)

您正尝试在onCreate()中调用mMap方法,但mMap将为null,直到您在onMapReady()中指定它为止。

您必须等到之后分配它(可能稍后在onMap ready或随后运行的某些内容中)来调用诸如设置事件侦听器的方法。