Null pointer exception at mapFragment.getMapAsync(this) while initializing google map
Asked Answered
F

6

10

When I use v6.5.87 of Google Play Services, I get a null pointer exception when I call getMapAsync(). I'm using a SupportMapFragment in my Fragment's xml layout.

My code:

SupportMapFragment mapFragment = (SupportMapFragment) getActivity()
        .getSupportFragmentManager().findFragmentById(R.id.mapFragment);

mapFragment.getMapAsync(this);

Note: There's no problem when calling getMap() on older Google Play Services versions, but getMap() is now deprecated (docs).

Here's the stack trace for the NPE:

java.lang.NullPointerException
            at com.decos.fixi.fragments.FragmentLocationMap.initializeMap(FragmentLocationMap.java:132)
            at com.decos.fixi.fragments.FragmentLocationMap.onCreateView(FragmentLocationMap.java:95)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4921)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
            at dalvik.system.NativeStart.main(Native Method)
Fugazy answered 23/1, 2015 at 11:46 Comment(0)
F
32

Finally I got the answer!

SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
                .findFragmentById(R.id.mapFragment);

instead of

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

getChildFragmentManager() did the trick.

Fugazy answered 27/1, 2015 at 11:22 Comment(3)
getChildFragmentManager() this method is not find.Mclaurin
Awesome! Worked for me as well.Vittoria
Genius! Was looking for ages how to do this in my fragment.Kataway
C
3

In addition, If you are using SupportMapFragment mapFragment = (SupportMapFragment).....

Then be sure you have set this class attribute in XML

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

Not

class ="com.google.android.gms.maps.MapFragment"
Compaction answered 25/4, 2016 at 5:3 Comment(0)
L
1

In AppCompatActivity extend

FragmentManager mFragmentManager = getSupportFragmentManager();
SupportMapFragment mVTMapFragment =(SupportMapFragment)mFragmentManager.findFragmentById(R.id.mapRideview);
mVTMapFragment.getMapAsync(this);

and implement onMapReady callback method

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    setUpMap();
}
Lubbock answered 16/6, 2016 at 5:51 Comment(0)
A
0

Have you implement the callback function for your getMapAsync()?

If not, You need to implement the onMapReady() method in your code, and then you can do adding markers or moving camera stuff in your onMapReady() method.

You can see this example about onMapReady().

Agosto answered 23/1, 2015 at 17:49 Comment(1)
Thanks for reply ztan. But I've implemented OnMapReadyCallback which overrides onMapReady() method!Fugazy
J
0

Heading

run your program by adding this

 SupportMapFragment supportMapFragment

ADD THIS in ON CREATE METHOD()

supportMapFragment = SupportMapFragment.newInstance(); 

FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.maplayout, new MapFragmentClass()).commit();
supportMapFragment.getMapAsync(this);

android.support.v4.app.FragmentManager sfm = getSupportFragmentManager();
sfm.beginTransaction().add(R.id.map,supportMapFragment).commit();

this will work check it

Janettajanette answered 7/7, 2016 at 7:37 Comment(0)
O
0

that will work only inside of a fragment. For those who have activity and not fragment getChildFragmentManager() will not work.

Simply use

  if (mapFragment != null) {
            mapFragment.getMapAsync(this);
        }
Oread answered 12/3, 2019 at 19:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.