NullPointerException : Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync

允我心安 提交于 2021-02-10 15:54:55

问题


I'm using com.google.android.gms:play-services-maps:11.0.2 version of Google Map Service. When trying to call the below, I get the following error:

java.lang.NullPointerException: Attempt to invoke virtual method
void

com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference

Below is my code:

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

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

}

回答1:


Looks like you are using SupportMapFragment instead of MapFragment.

Change this line:

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

To this:

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



来源:https://stackoverflow.com/questions/44867143/nullpointerexception-attempt-to-invoke-virtual-method-void-com-google-android

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