xxx cannot be resolved or is not a field

本文介绍了解决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。

确保语法没有错误。

`hostname.session cannot be resolved or is not a field` 错误通常是因为代码中尝试访问 `hostname` 对象的 `session` 字段,但该字段并不存在。在原代码里,`hostname` 是 `verify` 方法的一个参数,它是一个 `String` 类型,而 `String` 类型并没有 `session` 字段,所以会出现此错误。 原代码中 `HostnameVerifier` 的 `verify` 方法如下: ```java HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }); ``` 这里的 `hostname` 是主机名的字符串,`session` 是 `SSLSession` 对象。若代码中误写成 `hostname.session` 就会报错。正确的做法是直接使用 `session` 参数。 下面是修正后的代码示例: ```java import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLSession; import java.io.IOException; import java.net.URL; public class SecureFileDownloader { // ... 其他代码 ... // 设置安全的 HTTPS 连接,跳过 SSL 验证 private java.net.URLConnection setupSecureConnection(URL url) throws java.security.NoSuchAlgorithmException, java.security.KeyManagementException { javax.net.ssl.SSLContext sslContext = javax.net.ssl.SSLContext.getInstance("TLS"); CustomTrustManager trustManager = new CustomTrustManager(); sslContext.init(null, new javax.net.ssl.TrustManager[]{trustManager}, null); HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { // 这里直接使用 session 参数,而不是 hostname.session return true; } }); return url.openConnection(); } // ... 其他代码 ... } // 自定义信任管理器,用于跳过 SSL 验证 class CustomTrustManager implements javax.net.ssl.X509TrustManager { public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws java.security.cert.CertificateException { // 不做验证 } public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws java.security.cert.CertificateException { // 不做验证 } public java.security.cert.X509Certificate[] getAcceptedIssuers() { return new java.security.cert.X509Certificate[0]; } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值