0

I have a class with map, and took coordinates and title, from Firebase in String format, then put them into double, but can't place them to new LatLng? I understand, that problem is that googleMap = null, but re-tried all the ways to solve it. In this version of code, nullexception is disapearing, but marker is empty. Help please. It the last problem of my app..

public class Map_activity extends FragmentActivity implements OnMapReadyCallback{

    GoogleMap googleMap;


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

        // 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);
        String value = getIntent().getExtras().getString("qwerty");
        String xlat = getIntent().getExtras().getString("lat");
        String xlon = getIntent().getExtras().getString("lon");
        xlat.split(",");
        xlon.split(",");
        String [] latlong = {xlat, xlon};
        double latitude = Double.parseDouble(latlong[0]);
        double longitude = Double.parseDouble(latlong[1]);
        if (googleMap != null){
        LatLng location = new LatLng(latitude, longitude);
        googleMap.addMarker(new MarkerOptions().position(location).title(value));
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(location));}           
    }

    public void onMapReady(GoogleMap googleMap) {
        this.googleMap = googleMap;


    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Spike Johnson
  • 365
  • 4
  • 12

1 Answers1

1

onCreate is getting called before onMapReady. If the map is not ready, then the only value googleMap can be is null.

The only solution is to move the code that needs googleMap out of onCreate and put it somewhere else.

user234461
  • 1,133
  • 12
  • 29
  • Ok, i'm going to try this @user234461. The reason, that i put it in OnCreate, that i ssw an answere with similar problem, and this was solution, but not for me. – Spike Johnson Apr 27 '18 at 11:20
  • could you link to that answer? – user234461 Apr 27 '18 at 11:22
  • https://stackoverflow.com/questions/43545527/how-to-retrieve-data-from-firebase-to-google-map @user234461, earlier it seemed to me that our problems are similar – Spike Johnson Apr 27 '18 at 11:40
  • Big thanks my dear @user234461, you make my day! – Spike Johnson Apr 27 '18 at 11:41
  • The reason that the answer you linked to doesn't suffer from the same problem is that instead of running the code involving `googleMap` immediately, it creates an event listener which runs the code later (`onDataChange`). However, this assumes that the map will be ready before the data change happens, which is not necessarily true and could cause bugs. So the code in that answer is potentially buggy. – user234461 Apr 27 '18 at 12:41
  • I will keep in mind, thanks again for the help@user234461 – Spike Johnson Apr 27 '18 at 12:48