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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:07:51  来源:igfitidea点击:

Android Studio cannot resolve symbol but code executes correctly

javaandroidintellij-ideaandroid-activityandroid-studio

提问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 一直告诉我它无法“解析我正在使用的类的符号”。请看下面的截图:

SDK class - cannot find symbol

SDK 类 - 找不到符号

Click here for full-size image

单击此处查看全尺寸图像

The code builds and executes successfully. The function isThisThingWorking()simply returns true, and that is just what boolean blahgets set to.

代码构建并成功执行。该函数isThisThingWorking()只是返回true,而这正是boolean blah设置为。

I have tried pressing the Sync Project with Gradle Filesbutton and using the Invalidate Caches / Restart...option from Android Studio's Filemenu, 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.jarwas not generated correctly. It included .javafiles instead of .classfiles. 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 .javafiles.

我发现了问题 - 我的SDK.jar没有正确生成。它包含.java文件而不是.class文件。这解释了为什么 IDE 无法找到 SDK 类。Jar 中的包结构仍然是正确的,这就是为什么包名本身不是红色的原因。代码工作正常,因为编译器知道要编译.java文件。

To solve the issue, I modified my build.gradleof my SDK project to include .classfiles, instead of .javafiles, 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 Librariesoption 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.gradlesynchronization.

该解决方案是修改和强制一个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

enter image description here

在此处输入图片说明

Now select Language Injection Settingsand a window will open as shown below.

现在选择Language Injection Settings,将打开一个窗口,如下所示。

enter image description here

在此处输入图片说明

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:字段中选择了正确的文件类型并取消选中与错误代码相关的选项,然后单击确定

enter image description here

在此处输入图片说明

Hope this helps somebody.

希望这可以帮助某人。

回答by 0xAliHn

Try following steps:

尝试以下步骤:

  1. Close your project then in your project folder delete project /.idea
  2. Delete /.gradlefolder also
  3. Noe choose Fileoption from menu on left top side of android studio
  4. Select the option: Invalidate Cache/RestartIt will open a dialog
  5. Click on the first button with option: Invalidate and Restart
  1. 关闭您的项目,然后在您的项目文件夹中删除项目 /.idea
  2. /.gradle也删除文件夹
  3. NoeFile从android studio左上角的菜单中选择选项
  4. 选择选项:Invalidate Cache/Restart它将打开一个对话框
  5. 单击带有选项的第一个按钮: 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