【问题标题】:cannot cast 'android.support.v4.app.Fragment' to 'com.google.android.gms.maps.SupportMapFragment'无法将“android.support.v4.app.Fragment”转换为“com.google.android.gms.maps.SupportMapFragment”
【发布时间】:2019-08-17 09:08:33
【问题描述】:

我用谷歌地图活动创建了一个新项目,并安装了谷歌播放服务。我添加了互联网,精细位置和粗略位置的权限。 我复制了 google_maps_api.xml 中提到的 URL,并生成了 google_maps_api.xml 和清单元文件中使用的 API key.API 密钥。 现在我遇到了有关 supportMapFragment 的问题。

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}}

If i did Migrating app to AndroidX,then the errors are coming.Please click on this link to see errors

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.google.android.gms:play-services-maps:17.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

implementation 'androidx.core:core:1.0.0'
implementation 'androidx.legacy:legacy-support-core-ui:1.0.0'
implementation 'androidx.fragment:fragment:1.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'

}

我该如何解决这个问题。请帮助我。

【问题讨论】:

  • 将您的应用迁移到 AndroidX 可以解决此问题,请参阅 stackoverflow.com/questions/56963327/…
  • 我也这样做了。那个问题已经消失了,但出现了一些新问题。即 1) 无法解析 oncreate android oncreate(android.os.bundle)。 2) 无法解析方法 'setContentView(int)' 3) 无法解析符号 'FragmentActivity' 4) 无法解析方法 'getSupportFragmentManager()'
  • 你的app gradle的compileSdkVersionbuildToolsVersion是什么版本? @Priyanka
  • compileSdkVersion 28 defaultConfig { applicationId "com.example.googlemaps1" minSdkVersion 15 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" }
  • 请发送 gradle 依赖项? @Priyanka

标签: android google-maps android-studio supportmapfragment


【解决方案1】:

迁移到AndroidX,然后替换

import android.support.v4.app.FragmentActivity;

import androidx.fragment.app.FragmentActivity;

【讨论】:

  • 我按照你的回答。它显示“应用程序安装失败”错误。安装失败,消息“cmd package install-create -r -t -S 2026368”返回错误“未知故障:找不到服务:包”。此问题可能通过卸载现有版本的 apk(如果存在)得到解决,然后重新安装。警告:卸载将删除应用程序数据!是否要卸载现有应用程序?
  • 因为您没有完全迁移到 androidx ,并且在 app 文件夹中的 build.grade 下一行: implementation 'androidx.core:core:1.0.0' implementation 'androidx.legacy:legacy-support -core-ui:1.0.0'实现'androidx.fragment:fragment:1.0.0'实现'androidx.appcompat:appcompat:1.0.2'
  • 我添加了上述行并进行了同步。同样的错误再次出现
【解决方案2】:

如果您想使用 androidX,您必须从 Android 支持库迁移到 Jetpack (AndroidX) 库。

  • 将 com.android.tools.build:gradle 升级到 v3.2.1 或更高版本。
  • 将 compileSdkVersion 升级到 28 或更高版本。
  • 更新您的应用以使用 Jetpack (AndroidX); Migrating to AndroidX

这是来自youtube的视频

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多