getMap() returning null

2019-08-31 19:40发布

My getMap() method is returning null. Why is this method returning null? I looked on the Android Developers documentation to make sure I'm using this method correctly. Here a the line of code that I'm using to get the map:

mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.myMapView)).getMap();

Here is my Java code:

private ProjectionProxy proxy = new ProjectionProxy();

private GoogleMap mMap;

@Override
protected void onCreate(Bundle load) { 
    super.onCreate(load);

    setContentView(R.layout.fragment_ride_tracking);

    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.myMapView)).getMap();

    Log.e("RideTracking", "Google Map VALUE:"+mMap);

    if (mMap != null) { 
        proxy.setProjection(mMap.getProjection());
    }

Here is my XML code for that particular piece of code:

   <fragment 
    android:id="@+id/myMapView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.google.android.gms.maps.MapFragment"/>

Here is the logcat output:

05-09 12:49:34.282: W/GooglePlayServicesUtil(5928): Google Play services out of date.  Requires 3027100 but found 2012110
05-09 12:49:34.282: E/RideTracking(5928): Google Map VALUE:null
05-09 12:49:34.302: E/OGT.GPSLoggerServiceManager(5928): No GPSLoggerRemote service connected to this manager
05-09 12:49:34.322: W/dalvikvm(5928): Unable to resolve superclass of Lcom/android/gpstracker/viewer/FixedMyLocationOverlay; (2174)
05-09 12:49:34.322: W/dalvikvm(5928): Link of class 'Lcom/android/gpstracker/viewer/FixedMyLocationOverlay;' failed
05-09 12:49:34.322: W/dalvikvm(5928): Unable to resolve superclass of Lcom/android/gpstracker/viewer/FixedMyLocationOverlay; (2174)

Any help would be greatly appreciated!

2条回答
我想做一个坏孩纸
2楼-- · 2019-08-31 19:55

Apparently the line

05-09 12:49:34.282: W/GooglePlayServicesUtil(5928): Google Play services out of date.  Requires 3027100 but found 2012110

tells us why the map isn't instantiated.

You need to have a current version of Google Play services on your device or else you get this:
This app won't run without Google Play Services
and getMap() will return null.

查看更多
【Aperson】
3楼-- · 2019-08-31 20:05

Try Changing it like that

GoogleMap googleMap;

MapFragment fm = (MapFragment) getFragmentManager().findFragmentById(R.id.myMapView);


googleMap = fm.getMap();

If you are running it in Emulator you need to install two apk files (Read this Post)

If you have tweaked the Emulator and installed the two files already then you have to change the Google_Play_services_lib to version 4 (download it here)

Just replace the new Google_Play_services_lib with the old version#4 in eclipse and reference this to the project.

查看更多
登录 后发表回答