Google Maps Fragment returning null inside a fragment

前端 未结 8 924
花落未央
花落未央 2020-11-27 18:05

So I have an empty fragment that contains a map fragment. Whenever I try to activate the fragment containing the map, my app crashes and returns a null pointer error on this

8条回答
  •  一个人的身影
    2020-11-27 18:40

    This issue happened to me when I tried to port my Eclipse project to Android Studio project.

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

    I used it with a Fragment Activity like this:

    ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.mapToday)).getMap();
    

    However, the problem was, earlier I was using a specific version of Google Play Services (4.3.23).

    But the Gradle build select the most recent version (compile 'com.google.android.gms:play-services:+')

    I tried some other specific version like (6.1.71). But still gave null point exception.

    I used the same old version as I used before and it worked.

    compile 'com.google.android.gms:play-services:4.3.23'
    

    This works for me as a temporary solution. However, this will be a problem when I need to update to a new play services library.

    It seems that there is no specific map library with this version available for Gradle build when I try like this:

    compile 'com.google.android.gms:play-services-maps:4.3.23'
    

提交回复
热议问题