Java 错误:在 Android Studio 中找不到符号类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25332525/
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-11 00:07:03  来源:igfitidea点击:

error: cannot find symbol class in Android Studio

javaandroidandroid-studio

提问by user2087530

I'm having major problems with building my project.

我在构建项目时遇到了重大问题。

My project is getting built in Android Studio, and the directory is like so:

我的项目是在 Android Studio 中构建的,目录如下:

root
-project
--build
--libs
--src
---java
----com
-----company.project
------<All this projects source>
-projectutils
--build
--src
---main
----java
-----com.company.utils
------<All this projects source>

"project" is the main app, and it includes projectutils as a dependency. It uses Gradle to build, and I've added the correct includes to the root settings.gradle, and the correct dependencies to project.

“project”是主应用程序,它包含 projectutils 作为依赖项。它使用 Gradle 来构建,我已经将正确的包含添加到根 settings.gradle,以及正确的项目依赖项。

Here is my settings.gradle in the root of my project:

这是我的项目根目录中的 settings.gradle:

include ':project', ':projectutils'

Here is my build.gradle for my project:

这是我的项目的 build.gradle:

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion '20.0.0'
    defaultConfig {
        applicationId 'com.company.project'
        minSdkVersion 15
        targetSdkVersion 17
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    productFlavors {
    }
}

dependencies {
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
    compile files('libs/android-support-v4.jar')
    compile files('libs/commons-validator-1.4.0.jar')
    compile 'com.viewpagerindicator:library:2.4.1@aar'
    compile 'joda-time:joda-time:2.3'
    compile project(':projectutils')
}

No matter what I do, it always gives the error Error:(4, 34) error: package com.company.utils.database does not exist, even though I've added the correct dependency and the Android auto-complete can see and parse the import command, so obviously Android studio is able to see it correctly. There are a ton more errors, but they are all fundamentally the same: It can't seem to find com.company.utils and I have no idea what else I need to do.

无论我做什么,它总是给出错误 Error:(4, 34) 错误:包 com.company.utils.database 不存在,即使我添加了正确的依赖项并且 Android 自动完成可以看到和解析导入命令,很明显Android studio 能够正确地看到它。还有很多错误,但它们基本上都是一样的:它似乎找不到 com.company.utils,我不知道我还需要做什么。

I've spent all day trying to get this thing set up just to get it running and this is the biggest brick wall so far. I have literally no idea what I could be missing to cause these errors. I've fixed a bunch of similar errors with Facebook and Kumulos but this one won't work no matter what I try. Could anyone give me some pointers?

我花了一整天的时间试图设置这个东西只是为了让它运行,这是迄今为止最大的砖墙。我真的不知道我可能会遗漏什么导致这些错误。我已经用 Facebook 和 Kumulos 修复了一堆类似的错误,但无论我怎么尝试,这个错误都不起作用。谁能给我一些指点?

回答by esme_louise

I usually just start restarting and rebuilding things until something "takes". If you have Android Studio start with "Sync Project with Gradle Files" (there should be an icon for it in the ribbon by default). Then try rebuilding the project. Then try closing and checking the Windows Task Manager to make sure studio64.exe has completely closed and the process has stopped running, then restart Android Studio. Also restart the device you're using for testing. That's worked for me every time I encountered an error that shouldn't be happening.

我通常只是开始重新启动和重建事物,直到某些事情“发生”。如果您的 Android Studio 以“Sync Project with Gradle Files”开头(默认情况下,功能区中应该有一个图标)。然后尝试重建项目。然后尝试关闭并检查 Windows 任务管理器以确保 studio64.exe 已完全关闭且进程已停止运行,然后重新启动 Android Studio。还要重新启动您用于测试的设备。每次我遇到不应该发生的错误时,这都对我有用。

回答by user2087530

I figured out the issue: I had apply plugin: 'android'when I should have had apply plugin: 'android-library'I must have made copy-paste error when editing the gradle file of projectutils.

我发现了这个问题:我apply plugin: 'android'应该apply plugin: 'android-library'在编辑 projectutils 的 gradle 文件时出现了复制粘贴错误。

回答by Bhanu Pratap Singh

Change the

改变

compile 'com.google.android.gms:play-services:6.5.87'

by

经过

compile 'com.google.android.gms:play-services:6.1.71'

in dependencies in gradle file {build.gradle file}.

在 gradle 文件 {build.gradle file} 的依赖项中。

回答by Jorgesys

The error message: error: cannot find symbol class in Android Studiois related to correct versiones installed, for example this raise the exception:

错误消息:错误:在 Android Studio 中找不到符号类与安装的正确版本有关,例如这引发了异常:

compile 'com.google.android.gms:play-services-analytics:7.0.0'
compile 'com.google.android.gms:play-services-gcm:7.0.0'

but making a review of what version correspond to my last update of Google Play Services, see this information: https://developers.google.com/android/guides/setup

但是要查看与我上次更新的 Google Play 服务对应的版本,请参阅以下信息:https: //developers.google.com/android/guides/setup

i had to change :

我不得不改变:

compile 'com.google.android.gms:play-services-analytics:8.3.0'
compile 'com.google.android.gms:play-services-gcm:8.3.0'

now the proyect compile with no problem.

现在项目编译没有问题。

回答by Fruit

I have this problem just now, the reason is because I accidentally create the class by menu item New -> Fileinstead of New -> Java Class.

我刚才有这个问题,原因是我不小心通过菜单项New -> File而不是New -> Java Class创建了类。

I insert file name without .javaextension, since there is a dialog to allow me to choose Java, so I don't realize my mistake at this point: enter image description here

我插入不带.java扩展名的文件名,因为有一个对话框允许我选择Java,所以此时我没有意识到我的错误: 在此处输入图片说明

I add packageheader code manually, and all seems works fine (able ctrl+click to inspect), but it keep showing "cannot find symbol" for that class when trying to build.

package手动添加标题代码,一切似乎都很好(可以ctrl+单击检查),但是在尝试构建时它一直显示该类的“找不到符号”。

The issue is because this method create file name without .javaextension.

问题是因为此方法创建的文件名没有.java扩展名。

If you get similar error, try to check the file name extension.

如果出现类似错误,请尝试检查文件扩展名。