Android cannot find symbol method getMap() [duplicate]
Asked Answered
H

5

9

I have a gps app that works fine on my phone. However, I see there were people having a null pointer exception in the production.

    SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

    // Getting GoogleMap object from the fragment
    googleMap = fm.getMap();

    // Enabling MyLocation Layer of Google Map
    googleMap.setMyLocationEnabled(true);

    googleMap.setPadding(0, 0, 0, 90);

The error I'm getting from production is Caused by:

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.setMyLocationEnabled(boolean)' on a null object reference

So I did some research and find out that getMap method has been removed and people recommend using getMapAsync(this);

@Override
public void onMapReady(GoogleMap googleMap) {
    this.googleMap = googleMap;    //which doesn't seem to work
}

However, I'm referencing the googleMap variable in many different methods, and I cannot move all the code to onMapReady method. I tried using this.googleMap = googleMap inside the onMapReady method, but I'm still getting null pointer exceptions whenever I try to reference the googleMap variable. I defined the getMapAsync(this) in onCreated(), and then try to access the 'googleMap' object in onResume() and getting the null pointer exception. Is there a way to fix this? Thank you.

Haggar answered 17/7, 2016 at 3:19 Comment(2)
Can you show the layout file ?Wellman
The whole purpose of Async method is to run your code when the operation is ready which is done asynchronously. So you cannot and should not run any code before that callback is called. You need to refactor your login to execute post that callback.Comus
E
8

You are getting null pointer exception because you are trying to use google map object but google map object is still null,so you need to follow this step. write this code in onCreate() method

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

now impleents your class with public class Demo implements OnMapReadyCallback in short you need to override onMapReady(Googlemap googleMap) methood. you can perform all action after callback from onMapReady().

@Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
 // Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
    }
Endplay answered 17/7, 2016 at 5:5 Comment(2)
I created the above object in onCreate(), however, when I tried to use the googleMap object in onResume(), I'm stilling getting the null pointer exception.Haggar
You are getting null pointer exception in onResume() because your map is not ready yet.Risky
S
4

As I suggest in this Answer.

getMap() is deprecated use getMapAsync() instead of it in the following way.

Replace this

googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap)).getMap();

with this

googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap)).getMapAsync(this);

for more details visit this link

Subatomic answered 17/7, 2016 at 4:25 Comment(1)
The return type of getMapAsync(new OnMapReadyCallBack(){}) is void.Vocalize
B
2

You can use the getMapAsync(this) method. Please check this link for more

Boschbok answered 9/11, 2016 at 5:38 Comment(0)
O
1

Replace

googleMap = ((SupportMapFragment) getSupportFragmentManager()
    .findFragmentById(R.id.googleMap)).getMap();

with

googleMap = ((SupportMapFragment) getChildFragmentManager()
    .findFragmentById(R.id.map)).getMapAsync((OnMapReadyCallback) this);
Overbid answered 19/1, 2018 at 6:5 Comment(0)
W
0

Certainly you've mismatched the SupportMapFragment and MapFragment means if you have class attribute

class ="com.google.android.gms.maps.MapFragment"

in your XML then you should use

MapFragment mapFragment = (MapFragment) getFragmentManager() .findFragmentById(R.id.map);

OR

If you have class attr like

class="com.google.android.gms.maps.SupportMapFragment"

then you can do like as you are doing.

Wellman answered 17/7, 2016 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.