Google Map returning nullpointerexception Google Maps Android V2

前端 未结 3 1734
谎友^
谎友^ 2020-12-06 22:36

I have a google map in a fragment in my application. Funny thing is that the map loads, but the logcat displays a NullPointerException

相关标签:
3条回答
  • 2020-12-06 22:57

    MainActivity.xml :

        <fragment
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.google.android.gms.maps.SupportMapFragment"/>
    

    MainActivity.java :

    Private GoogleMap GoogleMap;
    googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    

    Here's the permission:

    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    

    And here's the metadata section:

    ...
    
    <meta-data android:name="com.google.android.maps.v2.API_KEY"
               android:value="HERE GOES YOUR API KEY" />
    

    0 讨论(0)
  • 2020-12-06 23:12
    googleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
    

    This worked for me. getSupportFragmentManager() seemed to be causing all the crashes.

    0 讨论(0)
  • 2020-12-06 23:13

    Just do like the below

    @Override
        public void onMapReady(GoogleMap googleMap) {
    
            MapsInitializer.initialize(getContext());
            map = googleMap;
            map.setMyLocationEnabled(true);
            // Updates the location and zoom of the MapView
            CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
            map.animateCamera(cameraUpdate);
        }
    
    0 讨论(0)
提交回复
热议问题