【问题标题】:How to import android support v4 correctly within mapactivity如何在 mapactivity 中正确导入 android support v4
【发布时间】:2019-07-02 16:24:17
【问题描述】:

我创建了空地图活动,添加了 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

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

【问题讨论】:

    标签: android android-studio android-fragmentactivity mapactivity


    【解决方案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;
    

    【讨论】:

    • 非常感谢!这很容易理解,现在andorid studio ges the Fragment从哪里来?如果我只添加了 androidX.appcompact 而不是 androidX.fragment
    • 当您添加 androidX 片段时,您只是添加了特定的片段依赖项。使用 appcompat,您可以添加所有内容。
    • 谢谢,有什么教程可以帮助我理解 gradle 文件和依赖项之间的关系吗? 'com.android.support.test:runner:1.0.2'呢,也应该改成androidX?
    • 不要一一手动更改这些依赖项,而是使用 Android Studio 中的“迁移到 AndroidX”功能。我不知道该选项的确切位置,但您可以谷歌如何迁移到 androidx。
    猜你喜欢
    • 2012-01-24
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多