可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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)
回答1:
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:
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.
回答2:
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
回答3:
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.
回答4:
Finally, here is the perfect answer
1.
Add
android.enableJetifier=true
android.useAndroidX=true
To the gradle.properties
2.
With android studio 3.2 higher migrate your project to AndroidX selecting Refactor > Migrate to AndroidX from the menu bar
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.Just replace them.
Done,You are good to go :-)
回答5:
I have just implemented the latest version of play-services-maps on the grade app module like below :
**
implementation 'com.google.android.gms:play-services-maps:17.0.0'
**
回答6:
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.
回答7:
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
回答8:
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.
回答9:
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.
回答10:
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.