xxx cannot be resolved or is not a field

解决ID未定义或非字段错误
本文介绍了解决Android开发中出现的ID无法解析或不是字段错误的方法,包括检查和修正导入语句、更新Eclipse插件及SDK、清理项目、刷新文件等步骤。

参考文章:http://stackoverflow.com/questions/3296047/id-cannot-be-resolved-or-is-not-a-field-error

【1】Look at your import statements at the top. If you are saying import android.R, then there that is a problem. It might not be the only one as these 'R' errors can be tricky, but it would definitely definitely at least part of the problem.

如果有android.R,则删去import android.R。

【2】If that doesn't fix it, make sure your eclipse plugin(ADT) and your android SDK are fully up to date, remove the project from the emulator/phone by manually deleting it from the OS, and clean the project (Launch Eclipse->Project->Clean...). Sounds silly to make sure your stuff is fully up to date, but the earlier versions of the ADT and SDK has a lot of annoying bugs related to the R files that have since been cleared up.

确认Build Automatically,再试试Launch Eclipse->Project->Clean。

【3】Just FYI, the stuff that shows up in the R class is generated from the stuff in your project res (aka resources) folder. The R class allows you to reference a resource (such as an image or a string) without having to do file operations all over the place. It does other stuff too, but that's for another answer. Android OS uses a similar scheme - it has a resources folder and the class android.R is the way to access stuff in the android resources folder. The problem arises when in a single class you are using both your own resources, and standard android resources. Normally you can say import at the top, and then reference a class just using the last bit of the name (for example, import java.util.Listallows you to just write List in your class and the compiler knows you mean java.util.List). When you need to use two classes that are named the same thing, as is the case with the auto-generated R class, then you can import one of them and you have to fully qualify the other one whenever you want to mean it. Typically I import the R file for my project, and then just say android.R.whatever when I want an android resource.

Also, to reiterate Andy, don't modify the R file automatically. That's not how it's meant to be used.

最后试试File->Refresh和File->Save。

确保语法没有错误。

<think>嗯,用户遇到了一个AES相关的编译错误:“'cannot be resolved or is not a field'”。我需要先分析可能的原因,然后给出解决方案。首先,这个错误通常出现在Java中,尤其是在使用AES加密算法时。可能的原因有几个: 1. **类名或字段名拼写错误**:用户可能错误地输入了AES的常量名称。比如,正确的应该是`AES/CBC/PKCS5Padding`,但用户可能拼写成了其他形式,比如大小写错误或者少了斜杠。 2. **JDK版本问题**:某些旧版本的JDK可能不支持AES相关的算法名称,或者需要安装额外的扩展包(比如JCE Unlimited Strength Policy)。如果用户使用的是较旧的JDK版本,可能缺少必要的加密库,导致编译器找不到AES字段。 3. **缺少加密库依赖**:如果项目依赖中没有包含正确的安全提供者,比如Bouncy Castle,也可能导致无法解析AES相关的常量。 接下来,我需要根据这些可能的原因,给出具体的解决步骤。首先检查代码中的拼写是否正确,确认使用的常量是否正确。例如,在Java中,使用`Cipher.getInstance("AES/CBC/PKCS5Padding")`时,必须确保字符串准确无误。如果拼写正确,那么可能需要检查JDK版本,确保使用的是Java 7或更高版本,因为旧版本可能不支持某些模式。此外,JCE策略文件可能需要替换为无限制版本,以允许使用更强的加密算法,这在某些国家是受限制的。 另外,如果用户使用的是第三方库,比如Bouncy Castle,需要确保正确添加了依赖,并且在代码中注册了安全提供者。例如,通过`Security.addProvider(new BouncyCastleProvider())`来注册。 最后,如果以上方法都无效,可能需要检查IDE的配置,比如项目的JDK路径是否正确,或者是否有缓存问题导致未正确识别新添加的依赖。清理和重新构建项目有时可以解决这类问题。 在给出解决方案时,需要分步骤说明,让用户能够逐步排查问题。同时,可能需要提供代码示例,帮助用户对照检查自己的代码是否存在拼写错误。此外,引用相关的文档或错误修复案例也会有帮助,比如引用Oracle关于JCE策略文件的说明,或者Bouncy Castle的安装指南。 用户的问题中还提到参考了一些引用内容,虽然这些引用主要涉及其他错误(如Android环境变量、Oracle GoldenGate错误、异常抑制技术),但可以忽略这些内容,专注于AES相关的错误。需要确保回答紧扣用户的问题,不涉及无关的引用信息。 总结下来,回答的结构应该是先解释错误原因,然后分点列出解决方案,包括检查拼写、验证JDK版本、安装JCE策略文件、添加Bouncy Castle依赖等。最后,提供相关问题供用户进一步参考。</think>### AES 'cannot be resolved or is not a field' 错误解决方案 #### 1. 错误原因分析 此错误通常发生在以下场景: - **字段名称拼写错误**:例如尝试访问不存在的常量(如 `AES.ECB` 写成 `AES.ECB_MODE`)[^3]。 - **加密算法未正确配置**:未导入相关加密库(如 Bouncy Castle)或 JDK 版本不兼容。 - **安全策略限制**:未安装 Java 无限制强度加密策略文件(JCE Unlimited Strength Policy)。 --- #### 2. 逐步解决方案 **步骤 1:检查字段名称和语法** 确保代码中使用的 AES 模式名称正确。例如: ```java // 正确写法 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); // 错误示例(字段名拼写错误) Cipher cipher = Cipher.getInstance("AES/ECB_MODE"); // ECB_MODE 不存在 ``` **步骤 2:验证 JDK 版本** - 检查 JDK 版本是否 ≥ 1.7(旧版本可能不支持某些 AES 模式): ```bash java -version ``` - 若版本过低,需升级到 OpenJDK 11 或 Oracle JDK 8 以上。 **步骤 3:安装 JCE 无限制策略文件** - 从 Oracle 官网下载对应 JDK 版本的 [JCE Unlimited Strength Policy](https://www.oracle.com/java/technologies/javase-jce-all-downloads.html)。 - 替换 `$JAVA_HOME/jre/lib/security` 中的 `local_policy.jar` 和 `US_export_policy.jar`。 **步骤 4:添加 Bouncy Castle 依赖** 若使用第三方加密库(如 Bouncy Castle): - Maven 依赖: ```xml <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.70</version> </dependency> ``` - 代码中注册提供者: ```java Security.addProvider(new BouncyCastleProvider()); ``` --- #### 3. 验证代码示例 ```java import javax.crypto.Cipher; import java.security.Security; import org.bouncycastle.jce.provider.BouncyCastleProvider; public class AESExample { public static void main(String[] args) { try { Security.addProvider(new BouncyCastleProvider()); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding"); // 使用 Bouncy Castle 的填充模式 System.out.println("AES 初始化成功"); } catch (Exception e) { e.printStackTrace(); } } } ``` --- #### 4. 常见陷阱 - **IDE 缓存问题**:修改 JCE 策略文件后需重启 IDE。 - **依赖冲突**:多版本 Bouncy Castle 共存可能导致类加载异常,检查依赖树。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值