【问题标题】:Inconvertible types; 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-06-23 08:48:29
【问题描述】:

我创建了一个谷歌地图的活动,当错误弹出时,错误是在活动的构建完成后出现的。 错误是:

 Inconvertible types; cannot cast 'android.support.v4.app.Fragment' to 'com.google.android.gms.maps.SupportMapFragment'

在行中(在 MapsActivity 中):

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

而且我也在资源中输入了API密钥!!

进口:

import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.purabjain.memorableplaces">

    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality.
    -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:replace="android:appComponentFactory"
        android:appComponentFactory="whateverString">

        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/.
        -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

build.gradle:


android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.purabjain.memorableplaces"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    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'
}

地图活动:


import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

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);
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @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));*/
    }
}

api.xml:

    <!--
    TODO: Before you run your application, you need a Google Maps API key.

    To get one, follow this link, follow the directions and press "Create" at the end:

    https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=E6:56:90:82:E2:58:42:66:59:4C:5A:F2:E5:9B:64:06:EC:1E:05:49%3Bcom.purabjain.memorableplaces

    You can also add your credentials to an existing key, using these values:

    Package name:
    E6:56:90:82:E2:58:42:66:59:4C:5A:F2:E5:9B:64:06:EC:1E:05:49

    SHA-1 certificate fingerprint:
    E6:56:90:82:E2:58:42:66:59:4C:5A:F2:E5:9B:64:06:EC:1E:05:49

    Alternatively, follow the directions here:
    https://developers.google.com/maps/documentation/android/start#get-key

    Once you have your key (it starts with "AIza"), replace the "google_maps_key"
    string in this file.
    -->
    <string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">AIzaSyDaCq_mx5h741VnM-4tNkNmQiT70yf7ykw</string>
</resources>

【问题讨论】:

  • 这里为什么有C++标签?

标签: java


【解决方案1】:

我也遇到过这个问题,但事实证明新版本的谷歌地图使用的是 17.0.0 的 AndroidX。所以你应该使用 16.1.0

所以改变这个:

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

到这里:

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

另外,请确保您不使用任何使用 AndroidX 的设备:在我的情况下,我使用了这会导致麻烦:

implementation 'com.google.android.gms:play-services-location:17.0.0'

所以我也做了这个

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

这是版本列表; https://mvnrepository.com/artifact/com.google.android.gms/play-services-maps

对于正确版本的 google play 服务:https://developers.google.com/android/guides/releases

【讨论】:

  • 你节省了我的时间。非常感谢
【解决方案2】:

将这些添加到 build.gradle(Module:app) 文件中:

androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

【讨论】:

  • 请说明您的答案中的预期结果:)
【解决方案3】:

我在 'androidx.fragment.app.fragment' 到 'com.google.android.gms.maps.supportmapfragment' 上遇到了类似的问题,解决方案是更改谷歌地图的版本。

发件人:

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

收件人:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多