標籤:android style blog http java color 使用 strong
問題描述
R cannot be resolved to a variable
問題原因
android:id="@+id/15"
定義id時命名不規範導致了編譯錯誤,不能使用數字,須使用英文字母
切記不能再犯。
另外,可能導致R cannot be resolved to a variable的還有其他原因,很多時候是由於各種原因R.java沒有產生,排查方法如下
轉載自http://blog.csdn.net/jason0539/article/details/9708025
1. 檢查Android 的SDK是否丟失需要重新下載,檢查build path,把需要匯入的JAR包確認都匯入成功
2. 確保class沒有import Android.R,注意是不能有Android.R,可以有其他R檔案,比如你自己的包;
3. 錯誤class引用的layout的xml檔案沒有錯誤,經常是xml裡面有錯誤,導致R檔案無法正常產生
很多時候都是這個地方出錯,可以嘗試在layout 的xml檔案手寫添加一個控制項,看id能否在R.java中自動產生,如果不能,那很大可能就是這個layout 的xml檔案有問題,查看格式是否使用正確,或者包含什麼非法字串,或者調用到了不正確的字串,等等,可以使用排除法,挨個去掉控制項,直到發現error message消失或者id能在R.java中自動產生。記得以前經常因為android這個單詞拼錯而出現R檔案無法正常產生而總是R cannot be resolved to a variable
4. 檢查AndroidManifest.xml檔案,裡邊的package,layout設定檔,strings.xml等的字串全部書寫正確.
5.刪掉gen檔案夾,使R.java重新自動產生一次,如果不能產生,繼續檢查layout的xml檔案是否有如上不易發覺的問題
6.Clean project ,重新build,或者重新import project。
7.重啟eclipse
8.重啟電腦,以防Android 虛擬機器的問題
轉載自http://blog.sina.com.cn/s/blog_8a94d82b0101bdfb.html
真是奇怪,第一次匯入一個項目時,竟然沒有產生R.java檔案,結果程式中一大堆錯誤,全是R cannot be resolved to a variable。
結果上網搜了半天,結果是
第一個查到的方法:
很多人碰到建立新工程的時候出現 cannot be resolved to a variable的問題時沒法在第一時間得到最好的解決
其根本原因是r.java檔案沒有自動產生
關於一般的解決方案在網上很多人提出來了
比如CLEAN工程,CLEAN UP RESOURCE,Android Tools >> Fix Project Properties 等
但還是部分人看了網上的帖還不能解決,那麼請注意兩點:
1. 如果你在更新下載SDK,那麼先關閉,重啟ECLIPSE便可
2. 如果還不行,則更新完整ADT、SDK
本文出自 “清風柳絮” 部落格,請務必保留此出處http://binuu.blog.51cto.com/801317/775781
第二個查到的方法:
*Note: Eclipse sometimes likes to add an "import android.R" statement at the top of your files that use resources, especially when you ask Eclipse to sort or otherwise manage imports. This will cause your make to break. Look out for these erroneous import statements and delete them.*
While going through the Android sample tutorials, I would often use the Ctrl + Shift + O command to "Organize Imports" and generate any missing import statements. Sometimes this would generate the incorrect import statement which would hide the R.java class that is automatically generated when you build.
自我感覺應該是產生R.java的過程被打斷了或發生了意外,於是刪除原來的項目,重新匯入,
哦哦,這次真的就成了!自動產生了R.java檔案。