MapFragment or MapView getMap() returns null on Lollipop

前端 未结 4 1627
情话喂你
情话喂你 2020-12-09 09:32

I\'ve been using Google Maps API v2 for a long time on Android 4.x versions without a problem. Now I installed latest Lollipop build on my Nexus devices (5 and 7) trying to

相关标签:
4条回答
  • 2020-12-09 10:04

    It looks like this might be an issue with a targetSdkVersion of 21: https://code.google.com/p/android-developer-preview/issues/detail?id=1947

    However, switching to getChildFragmentManager() worked for me:

    findFragmentById for SupportMapFragment returns null in Android Studio

    0 讨论(0)
  • 2020-12-09 10:08

    Google now made a more convenient way to get the map using the following method

        myMapFragment.getMapAsync(new OnMapReadyCallback) {
             @Override
             public void onMapReady(GoogleMap googleMap) {
                 myMap = googleMap;
             }
        });
    
    0 讨论(0)
  • 2020-12-09 10:10

    I had exactly the same problem but this is what worked for me:

    Replace this...

    GoogleMap map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

    with this...

    GoogleMap map = getMapFragment().getMap();

    then slip this bad boy in and give it a whirl...

    private MapFragment getMapFragment() {
        FragmentManager fm = null;
    
        Log.d(TAG, "sdk: " + Build.VERSION.SDK_INT);
        Log.d(TAG, "release: " + Build.VERSION.RELEASE);
    
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            Log.d(TAG, "using getFragmentManager");
            fm = getFragmentManager();
        } else {
            Log.d(TAG, "using getChildFragmentManager");
            fm = getChildFragmentManager();
        }
    
        return (MapFragment) fm.findFragmentById(R.id.map);
    }
    
    0 讨论(0)
  • 2020-12-09 10:22

    Have you tried isGooglePlayServicesAvailable to check why its returning null? null has many reasons on getmap, try using this to check why its giving null

    0 讨论(0)
提交回复
热议问题