如何在 mapactivity 中正确导入 android 支持 v4

How to import android support v4 correctly within mapactivity

我创建了空地图 activity,添加了 KEY,并尝试构建但出现此错误: 无法解析符号 'FragmentActivity'

我尝试添加: 实施 'com.android.support:support-v4:28.0.0' gradle 和其他解决方案一样,但仍然有同样的错误

package com.example.praycomp;

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

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

...
}



apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.praycomp"
        minSdkVersion 17
        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.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.fragment:fragment:1.0.0'
    implementation 'com.android.support:support-v4:28.0.0'
}

我的 gradle.properties 里有这个:

android.useAndroidX=true
android.enableJetifier=true

我想了解为什么会看到此错误以及如何解决它

您将 Androidx 依赖项与 Android Support library 依赖项混合在一起。你有 useAndroidX 为真,所以使用 Androidx 依赖项。

删除

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'

从您的 app/build.gradle 添加

implementation 'androidx.appcompat:appcompat:1.1.0-beta01'

然后导入 FragmentActivity 如下所示:

import androidx.fragment.app.FragmentActivity;