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

时间:2019-07-02 16:24:17

标签: android android-studio android-fragmentactivity mapactivity

我创建了空的地图活动,添加了KEY,然后尝试构建但出现此错误: 无法解析符号“ FragmentActivity”

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

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

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

1 个答案:

答案 0 :(得分:1)

您正在将Androidx个依赖项与Android Support library个依赖项混合在一起。您将useAndroidX设为true,因此请使用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;