我刚刚尝试使用Android Studio。我已经创建了空白项目,并试图创建扩展AppCompatActivity的活动。不幸的是,Android Studio“说”它
无法解析符号“AppCompatActivity”
我已经编译了“com.android.support:appcompat-v7 . 22.0”。在我的“应用程序”模块的依赖列表和重建项目几次。然而,我只能使用ActionBarActivity。我做错了什么?
我刚刚尝试使用Android Studio。我已经创建了空白项目,并试图创建扩展AppCompatActivity的活动。不幸的是,Android Studio“说”它
无法解析符号“AppCompatActivity”
我已经编译了“com.android.support:appcompat-v7 . 22.0”。在我的“应用程序”模块的依赖列表和重建项目几次。然而,我只能使用ActionBarActivity。我做错了什么?
当前回答
在尝试了所有解决方案后,我意识到我所从事的项目之前使用的是最新的Android Studio,当时是3.2,而我目前使用的pc在更新Android Studio后运行的是2.2,这似乎完全解决了我的问题。
解决方案:Android Studio ->检查更新,然后安装最新版本
其他回答
记住按Alt+Enter或添加导入。
import android.support.v7.app.AppCompatActivity;
所以我的同事推动了他们当地的。它破坏了一切。我复制了我的sdk路径从另一个应用程序,并做:
File -> Sync Project with Gradle Files
你可以使用新的appcompat-v7:22.1.+ 只要把这个添加到你的gradle:
编译“com.android.support: appcompat-v7:22.1.1”
这里是对其他答案的一点补充,适用于在使用正确的lib版本和正确的类时遇到相同错误的任何人。
当我升级到
appcompat-v7:22.1.0
其中ActionBarActivity已弃用,空和appcompatactivity是要走的路,由于在Android工作室的一些故障,它没有很好地拾取版本变化。
例如,即使Gradle运行时没有错误,IDE本身也一直在说 无法解析符号“AppCompatActivity”(它也不能通过Ctrl+N搜索)
我查看了.idea/libraries文件夹,注意到没有适合lib新版本的元文件。
因此,使用旧的可靠的File->Invalidate Caches/Restart就可以了。当你觉得Android Studio有什么神奇的错误时,总是尝试这个方法。 然后禁用离线模式和同步。
出现这种情况的原因如下:
你没有安装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.