Why can not I add a bookmark when I click on the map?

0

I'm using this code:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import static com.marcadorfixo.R.id.map;

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
    GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(map);
        mapFragment.getMapAsync(MainActivity.this);

        googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng position) {
                googleMap.addMarker(
                        new MarkerOptions().position(position)
                );
            }
        });
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        LatLng uaua = new LatLng(-9.840318, -39.480388);
        googleMap.addMarker(new MarkerOptions()
                .position(uaua)
                .title("Uauá"));
    }
}

Android Studio even compiles, but when trying to run the application it simply stops working.

    
asked by anonymous 23.01.2018 / 21:38

1 answer

1

The problem is that you are adding a click to a null object that was not instantiated. You can only add the click after receiving the object in the onMapReady method. Try the solution below:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import static com.marcadorfixo.R.id.map;

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
    //GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(map);
        mapFragment.getMapAsync(MainActivity.this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        LatLng uaua = new LatLng(-9.840318, -39.480388);

        googleMap.addMarker(new MarkerOptions()
                .position(uaua)
                .title("Uauá"));

        googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng position) {
                googleMap.addMarker(
                        new MarkerOptions().position(position)
                );
            }
        });
    }
}
    
24.01.2018 / 14:18