0

I had an old Android Project that I had left behind and I have decided to pick it up. But Im getting the "Cannot resolve method getMap()" error. I have tried doing whats here, here and here, but im getting stuck anyway. I know Im supposed to use getMapAsync, but I guess Im failing to implement that with MapView

I think maybe its because Im using MapView and not SupportMapFragment. I tried changing that as well but I failed along the way.

Here are he dependencies I want to use or rather im using:

compile 'com.google.android.gms:play-services-maps:10.0.1' //one with error
//compile 'com.google.android.gms:play-services-maps:7.3.0' //one which works

I have a few methods which are being flagged, as im using the getMap(), more than once, but the error is flagged here:

GoogleMap map = ((MapView) mRootView.findViewById(R.id.mapview)).getMap();

The MapView is displayed in the following:

<com.google.android.gms.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

Im getting the following error:

12-09 09:14:33.231 15831-15831/com.empire.vince.vokers.yoworld E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.empire.vince.vokers.yoworld, PID: 15831
                                                                             java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.maps.android.clustering.ClusterManager.clearItems()' on a null object reference
                                                                                 at com.empire.vince.vokers.yoworld.mafragments.MapFragment.renderView(MapFragment.java:465)
                                                                                 at com.empire.vince.vokers.yoworld.mafragments.MapFragment.onMapReady(MapFragment.java:433)
                                                                                 at com.google.android.gms.maps.MapView$zza$1.zza(Unknown Source)
                                                                                 at com.google.android.gms.maps.internal.zzt$zza.onTransact(Unknown Source)
                                                                                 at android.os.Binder.transact(Binder.java:387)
                                                                                 at zu.a(:com.google.android.gms.DynamiteModulesB:82)
                                                                                 at maps.ad.t$5.run(Unknown Source)
                                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 at android.os.Looper.loop(Looper.java:234)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5526)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

This is the first function of 4 which were using the GoogleMap object:

private void gimmeView()
{
    // reference

   //final GoogleMap map = ((MapView) mRootView.findViewById(R.id.mapview)).getMap();
    final AdView adView = (AdView) mRootView.findViewById(R.id.adview);

    // map
    if(mGoogleMap!=null)
    {
        // add pois
       mGoogleMap.clear();
        mClusterManager.clearItems();
        for(PoiModel poi : mPoiList)
        {
            mClusterManager.addItem(poi);
        }
        mClusterManager.cluster();
    }

    // admob
    if(YoWorldConfig.ADMOB_MAP_BANNER && NetworkManager.isOnline(getActivity()))
    {
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice(getString(R.string.admob_test_device_id))
                .build();
        adView.loadAd(adRequest);
        adView.setVisibility(View.VISIBLE);
    }
    else
    {
        adView.setVisibility(View.GONE);
    }
}
Community
  • 1
  • 1
Vincent H Guyo
  • 356
  • 6
  • 24
  • The answer in one of those links says "Please use getMapAsync()". You may want to scan over the latest Google maps documentation to spot differences in your implementation – OneCricketeer Dec 08 '16 at 14:17
  • Possible duplicate of [Replace getMap with getMapAsync](http://stackoverflow.com/questions/31371865/replace-getmap-with-getmapasync) – N Dorigatti Dec 08 '16 at 15:55

1 Answers1

0

Don't cast MapView to GoogleMap, here is what you should do :

private GoogleMap mGoogleMap;

OnCreate method :

mMapView = (MapView) view.findViewById(R.id.mapview);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
mMapView.getMapAsync(this);

Implement OnMapReadyCallback:

@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
if (mGoogleMap != null) {
    // Add your functions to GoogleMap
 }
}
Ahmed Abidi
  • 1,047
  • 12
  • 24