29
import com.google.android.gms.maps.SupportMapFragment;
import androidx.fragment.Fragment;
...
private SupportMapFragment mMapFragment;
...
mMapFragment = (SupportMapFragment) form.getSupportFragmentManager().findFragmentByTag(MAP_FRAGMENT_TAG);

Here, the IDE shows an error that androidx.fragment.Fragment cannot cast to SupportMapFragment (which extends android.support.v4.app.Fragment)

I'm using the version 15.0.1 of the play-services-maps and version 1.0.0-beta01 of the AndroidX libraries.

I have not found any updates to SupportMapFragment after AndroidX in the release notes: https://developers.google.com/maps/documentation/android-sdk/releases

Edit: Before anyone asks, form is an instance of Form class which extends AppCompatActivity (from AndroidX)

xomena
  • 31,125
  • 6
  • 88
  • 117
pavi2410
  • 1,220
  • 2
  • 12
  • 16
  • Similar problem caused by `implementation 'com.google.android.gms:play-services-maps:16.0.0'` solved by updating gradle to `classpath 'com.android.tools.build:gradle:3.2.0'` – Pavel Dec 15 '18 at 20:29
  • 1
    Be careful, it fails again with 'com.google.android.gms:play-services-maps:16.1.0', so stay attached to the version 16.0.0 until they fix this bug in future releases. – vikir789 May 06 '19 at 22:27

11 Answers11

17

This issue has already been reported to Google in the public issue tracker:

https://issuetracker.google.com/issues/110573930

I would suggest starring the feature request in the public issue tracker to add your vote and subscribe to further notifications from Google. Hopefully, Google will implement it in next versions of Android Maps SDK.

Update

Google has provided the following answer in the public issue tracker

Hi all,

As noted the Google Maps API is currently based off the support libraries and not AndroidX. Future versions of Google Maps API will certainly support AndroidX, but in the meantime, the following workaround will resolve the issue for you:

  • Use Android Studio 3.2 (currently preview) or higher

  • Ensure your build.gradle contains 'compileSdkVersion 28' (default for new AS 3.2 projects)

  • Include the following in your gradle.properties file (default for new AS 3.2 projects)

    android.useAndroidX=true

    android.enableJetifier=true

Finally, for existing code referencing the support libraries, you can use the "Refactor -> Refactor to AndroidX" tool in Android Studio 3.2+ to automatically convert your code to use the new AndroidX packages.

Community
  • 1
  • 1
xomena
  • 31,125
  • 6
  • 88
  • 117
16

I had your same problem, fixed by updating all imports to the last version available in the build.gradle file and adding

android.enableJetifier=true
android.useAndroidX=true

To the gradle.properties

this is the version of the maps package I imported

implementation 'com.google.android.gms:play-services-maps:15.0.1'

You just have to fix some imports and then you're done

I hope

EDIT: even after editing what i just said, you will still have the error shown in the java but it compiles and runs without errors

EDIT2: yesterday they suggested a workaround on the issuetracker https://issuetracker.google.com/issues/110573930#comment13

Marco Tondini
  • 162
  • 1
  • 8
6

Finally, here is the perfect answer:

  1. Add

    android.enableJetifier=true
    android.useAndroidX=true
    

    to your gradle.properties file.

  2. With Android Studio 3.2 or higher, migrate your project to AndroidX by selecting Refactor > Migrate to AndroidX from the menu.

  3. (Most important) Some of your project imports (used in classes) will need to be replaced by equivalent AndroidX libraries. Here you can find libraries and their equivalents. Replace any imports that refer to pre-AndroidX libraries one-by-one.

Now you're good to go :-)

stkent
  • 19,772
  • 14
  • 85
  • 111
Asad Khan
  • 61
  • 1
  • 3
5

Add the latest google map play services and that'll solve the problem.

implementation 'com.google.android.gms:play-services-maps:16.0.0'

Google play services latest versions.

  • I don't think you understood the question. It's the same situation with 16.0.0 – clementiano Nov 04 '18 at 18:49
  • @clementiano I do. I had the similar issue. Then changing the version to 16.0.0 resolved the issue. – Raj kannan Iyyappan Nov 04 '18 at 19:30
  • I started out with 16.0.0 and had that issue so that could never have been a fix. After enabling jetifier and useAndroidX in gradle.properties the error is still there in Android studio but it will run, just as @marco-tondini pointed out on another answer. That should be the accepted answer – clementiano Nov 04 '18 at 19:59
5

I solved this by implementing the latest version of play-services-maps in my app's build.gradle file:

implementation 'com.google.android.gms:play-services-maps:17.0.0'
stkent
  • 19,772
  • 14
  • 85
  • 111
2

Android Jetifier unfortunately did not work for me. I solved this problem by copying SupportMapFragment to my project and changing the parent. During this procedure I also had to make copy of class called zzak (renamed it to SupportMapCallback).

Please note that you should take this solution as temporary cause right now it's independent on updates of Play Services library. If you also need to migrate to AndroidX as soon as possible then feel free to grab it from here:

https://gist.github.com/mroczis/3988e390a8b04fad3c8714181718cd60

mroczis
  • 1,879
  • 2
  • 16
  • 18
2

For this to work you must be on the following version or higher:

com.google.android.gms:play-services-maps:16.0.0

After migrating to AndroidX, be sure to invalidate caches and restart to get rid of all the red errors on the Google Maps dependencies.

Mike
  • 10,297
  • 2
  • 21
  • 21
  • Thanks for this answer, I have been looking for days for a solution and `invalidate caches and restart` fixed my issue as I already was using `16.0.0` when I migrated to AndroidX. – user2101081 Jan 14 '19 at 22:31
1

You need to update the Android studio version to more than 3.2 it's still a beta version. The problem is Google Maps library is using Fragment class from the support library so it's conflicted with androidX, so Android Studio 3.2 version make refactor all dependencies to use androidX classes.

Mohamed ElSawaf
  • 229
  • 2
  • 8
0

I had this issue too. It is really not nice, and for sure temporary, but that was the only way I could make it work. So at least it unblocked me until it gets fixed.

    final Object mf = getChildFragmentManager().findFragmentById(R.id.map);
    if (mf instanceof SupportMapFragment) {
        final SupportMapFragment smf = (SupportMapFragment) mf;
        // ...
    } else {
        //handle
    }

It works both compiler time and runtime.

0

Make sure that in the fragment java class you import androidx.fragment.app.Fragment; and in the java class from where you refer to this particular fragment, you also import Fragment the same way i.e. import androidx.fragment.app.Fragment;

After that to get a reference of the fragment we created, use getSupportFragmentManager().findFrgmentById(R.id.fragment_id).

To summarise, make sure you have the same import in all concerned Java or Kotlin classes.

Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39
0

One thing to remember when switching from a MapFragment to SupportMapFragment is to specify in the xml layout that the fragment is actually a SupportMapFragment class="com.google.android.gms.maps.SupportMapFragment":

<fragment
    android:id="@+id/map"
    class="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

Then you can do:

(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

Looking at the error message you posted, I suspect this was the main issue in your case. The definition in your layout file is of type androidx.fragment.Fragment (which is the object type you get when you do findFragmentByTag or findFragmentById), but you are trying to cast it to com.google.android.gms.maps.SupportMapFragment.

For more reference, please see the SupportMapFragment class.

Dimitar Darazhanski
  • 2,188
  • 20
  • 22