SupportMapFragment不支持AndroidX Fragment

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

这里,IDE显示androidx.fragment.Fragment无法强制转换为SupportMapFragment(扩展android.support.v4.app.Fragment)的错误

我使用的是play-services-maps的15.0.1版和AndroidX库的1.0.0-beta01版.

在发布说明:https://developers.google.com/maps/documentation/android-sdk/releases中,我没有在AndroidX之后找到SupportMapFragment的任何更新

编辑:在有人问之前,form是一个Form类的实例,它扩展了AppCompatActivity(来自AndroidX)

解决方法:

Google已在公共问题跟踪器中报告此问题:

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

我建议在公共问题跟踪器中主演功能请求,以添加您的投票并订阅来自Google的进一步通知.希望Google能够在下一版Android Maps SDK中实现它.

更新

Google在公共问题跟踪器中提供了以下答案

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.

上一篇:android – 为什么butterknife 9.0.0-SNAPSHOT无法解析?


下一篇:总是听到有人说AndroidX,到底什么是AndroidX?Android和AndroidX的区别