我刚刚尝试使用Android Studio。我已经创建了空白项目,并试图创建扩展AppCompatActivity的活动。不幸的是,Android Studio“说”它

无法解析符号“AppCompatActivity”

我已经编译了“com.android.support:appcompat-v7 . 22.0”。在我的“应用程序”模块的依赖列表和重建项目几次。然而,我只能使用ActionBarActivity。我做错了什么?


当前回答

出现这种情况的原因如下:

你没有安装SDK API(这是我的问题) 你的项目/Android Studio不知道需要的文件在哪里 您的项目引用了不支持AppCompatActivity的过时编译版本 有个错别字

可能的解决方式:

Check your .gradle file to make sure you’re not referencing an outdated version. AppCompatActivity was added in version 25.1.0 and belongs to Maven artifact com.android.support:appcompat-v7:28.0.0-alpha1, so do not use any version earlier than this. In your build.gradle (Module: app) file you should have the dependency listed: dependencies { compile 'com.android.support:appcompat-v7:25.1.0' } You may be using a different version, but just make sure you have listed the dependency. Open the SDK manager and download every API 7 or newer. If you were missing the needed API it will fix that issue, and downloading all the newer API’s can save you some hassle later on as well. Check for typos in your import statement. Make sure it doesn’t say “AppCompactActivity” instead of “AppCompatActivity”. Resync your project (File >Sync project with Gradle files) Rebuild your project (Build >rebuild) Clean your project (Build >clean project) Close and restart Android Studio Update Android Studio Regenerate your libraries folder – In Android Studio, view your files in project view. Find the folder YourProjectName >.idea >libraries. Rename the folder to “libraries_delete_me”. Close and reopen Android Studio. Open your project again and the libraries folder should be regenerated with all of the files; you can now delete the “libraries_delete_me” folder.

其他回答

如果其他方法都失败了,请确保添加了import语句。我已经删除了它,因为它显示错误,忘记把它添加回来,并尝试其他一切来修复:

进口android.support.v7.app.AppCompatActivity;

升级到最新的支持库后,我不得不升级到最新的Android studio(测试版)版本和gradle版本,然后IDE才能再次识别AppCompatActivity(无效缓存和重新启动没有做到这一点)

这是我在某处找到的救生代码(对不起,不记得起源)。添加它来构建。gradle(模块:应用程序)。您可以将版本替换为相关的版本。

configurations.all {

    resolutionStrategy.eachDependency { DependencyResolveDetails details -
        def requested = details.requested

 if (requested.group == 'com.android.support') {

            if (!requested.name.startsWith("multidex")) {
                details.useVersion '27.1.1'
            }
        } else if (requested.group == "com.google.android.gms") {
            details.useVersion '15.0.0'
        } else if (requested.group == "com.google.firebase") {
            details.useVersion '15.0.0'
        }
    }
}

对我来说,没有一个解决办法。但是我发现所有的支持库在我的外部库中都不见了。在其他项目中,AppCompatActivity问题得到了解决。

对我来说,创建一个新项目并将旧src文件夹中的所有东西复制到新项目中是有效的。

我不知道确切的原因,但如果没有效果,这可能是最后的可能性。如果有人和我有同样的"麻烦"

尝试温和的方法行不通,那就用强硬的方法吧: 退出当前Android Studio屏幕,转到项目的。idea文件夹,重命名库->lib_pre文件夹。

重新启动Android Studio。现在它将重新创建libraries文件夹并再次顺利工作。 Android Studio 3.1.3