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

白昼怎懂夜的黑 提交于 2019-11-30 19:01:47

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.

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"
Krishna

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();
}

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().

Bhupendra Singh

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

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);
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!