Java Android Studio 无法解析符号但代码正确执行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18991161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Studio cannot resolve symbol but code executes correctly
提问by BVB
I have recently created a library Jar file that I imported in my Android project. I am able to call functions and use classes that are present in this Jar file, but Android Studio keeps telling me that it cannot "resolve symbol" of the class I am using. Please see the screenshot below:
我最近创建了一个库 Jar 文件,并将其导入到我的 Android 项目中。我能够调用函数并使用此 Jar 文件中存在的类,但 Android Studio 一直告诉我它无法“解析我正在使用的类的符号”。请看下面的截图:
Click here for full-size image
The code builds and executes successfully. The function isThisThingWorking()
simply returns true
, and that is just what boolean blah
gets set to.
代码构建并成功执行。该函数isThisThingWorking()
只是返回true
,而这正是boolean blah
设置为。
I have tried pressing the Sync Project with Gradle Files
button and using the Invalidate Caches / Restart...
option from Android Studio's File
menu, but none of this solved the issue. What can I do to make the Android Studio IDE not display the Cannot resolve symbol 'xxxSDK'
error?
我尝试按下Sync Project with Gradle Files
按钮并使用Invalidate Caches / Restart...
Android StudioFile
菜单中的选项,但这都没有解决问题。我该怎么做才能使 Android Studio IDE 不显示Cannot resolve symbol 'xxxSDK'
错误?
采纳答案by BVB
I found the issue - my SDK.jar
was not generated correctly. It included .java
files instead of .class
files. This explains why the IDE was not able to find the SDK class. The package structure was still correct in the Jar, which is why the package name itself is not a red color. The code worked correctly, because the compiler knew to compile the .java
files.
我发现了问题 - 我的SDK.jar
没有正确生成。它包含.java
文件而不是.class
文件。这解释了为什么 IDE 无法找到 SDK 类。Jar 中的包结构仍然是正确的,这就是为什么包名本身不是红色的原因。代码工作正常,因为编译器知道要编译.java
文件。
To solve the issue, I modified my build.gradle
of my SDK project to include .class
files, instead of .java
files, when creating the Jar. Including this new Jar instead of the old Jar fixed the IDE issue.
为了解决这个问题,我修改build.gradle
了我的 SDK 项目,在创建 Jar 时包含.class
文件,而不是.java
文件。包含这个新 Jar 而不是旧 Jar 修复了 IDE 问题。
EDIT: For most folks, Senthil's answerwill be more appropriate. I am leaving this one as the accepted answer because it solved my specific problem.
编辑:对于大多数人来说,Senthil 的回答会更合适。我将此作为已接受的答案,因为它解决了我的具体问题。
回答by free3dom
Try adding the library to the Project Structure
.
尝试将库添加到Project Structure
.
To do this, on the menu choose File
-> Project Structure
. Select the Libraries
option and click the green +
to add your library.
为此,请在菜单上选择File
-> Project Structure
。选择该Libraries
选项并单击绿色+
以添加您的库。
回答by Senthil
I have faced this issue when IntelliJ IDEA got closed abruptly due to OS crash.
当 IntelliJ IDEA 由于操作系统崩溃而突然关闭时,我遇到了这个问题。
You can do "File" -> "Invalidate Caches...", and select "Invalidate and Restart" option to fix this.
您可以执行“文件”->“使缓存无效...”,然后选择“无效并重新启动”选项来解决此问题。
回答by Machado
This was always happening to me after switching branches on my current project: a lot of non-sense erros being reported by the IDE.
在我当前项目上切换分支后,这总是发生在我身上:IDE 报告了很多无意义的错误。
The solution is to modify and forcea build.gradle
synchronization.
该解决方案是修改和强制一个build.gradle
同步。
回答by Tricky Bay
Changing the language injection settings worked for me.
更改语言注入设置对我有用。
Place the cursor on any of the red underlined code and
Alt + Enter
将光标放在任何红色下划线代码上,然后
Alt + Enter
Now select
Language Injection Settings
and a window will open as shown below.
现在选择
Language Injection Settings
,将打开一个窗口,如下所示。
Make sure you have the right file type selected in
ID:
field and Uncheck the options related to your error codes and click Ok
确保在
ID:
字段中选择了正确的文件类型并取消选中与错误代码相关的选项,然后单击确定
Hope this helps somebody.
希望这可以帮助某人。
回答by 0xAliHn
Try following steps:
尝试以下步骤:
- Close your project then in your project folder delete project
/.idea
- Delete
/.gradle
folder also - Noe choose
File
option from menu on left top side of android studio - Select the option:
Invalidate Cache/Restart
It will open a dialog - Click on the first button with option:
Invalidate and Restart
- 关闭您的项目,然后在您的项目文件夹中删除项目
/.idea
/.gradle
也删除文件夹- Noe
File
从android studio左上角的菜单中选择选项 - 选择选项:
Invalidate Cache/Restart
它将打开一个对话框 - 单击带有选项的第一个按钮:
Invalidate and Restart
After trying above options everything should be working fine.
尝试上述选项后,一切都应该正常工作。
回答by Sumit Kumar
This problem regularly generated in android 3.1. I have tried every possible way. But it resolved when i delete .gradleand .ideafile and start Android studio which is already close.
这个问题在android 3.1中经常产生。我已经尝试了所有可能的方法。但是当我删除.gradle和.idea文件并启动已经关闭的 Android studio时它解决了。
Android studio create .ideaand .gradlefile for your project again. After this click on sync project with gradle. Now everything works fine.
Android studio再次为你的项目创建.idea和.gradle文件。在此之后单击与 gradle 同步项目。现在一切正常。
回答by sedq
For me worked only
只为我工作
File -> Open -> and click build.gradle to reopen project
File -> Open -> 并点击 build.gradle 重新打开项目
回答by whateven
This worked for me:
这对我有用:
Go to File
-> Sync Project with Gradle Files
.
转到File
-> Sync Project with Gradle Files
。