问题
I was trying to work out with multiple layout using some online tutorials. The thing is my program was building and compiling correctly as long as there was only one XML file. When I added multiple XML files, I am getting errors saying 'activity_main cannot be resolved or is not a field' for the line setContentView(R.layout.activity_main);
In fact, every item which I used previously in R.*
is now reporting error. I don't know what went wrong. Can anyone suggest what needs to be done?
Edit 1: I tried cleaning and rebuilding the project but that didn't help. Plus, I am importing the correct R.java
file as in mypackage.R.java
According to @RubberDuck's comment, I as soon as I deleted the files again, the R.java file was being generated after building the project. However, when I create new XML files and add elements in it, build the project, R.java gets deleted.
回答1:
If your XML's contain errors your generated file might not build try erasing most of their content and making sure id's and file names are not duplicated and no capitals and spaces are used. If it works you can start adding stuff.
回答2:
I had the same problem, fixed by replacing R
with com.example.appname.R
obviously put your package reference in there instead. Or just add this line to your file:
import com.your.package.R
Or even better, try removing this line from your code if exists:
import android.support.compat.R
回答3:
Check if you not imported android.R accidentally.
回答4:
The stupid mistake I made to cause this error was to have the wrong package string in my AndroidManifest.xml
file. I'd left the string set in the tutorial, but had used my own when setting up the project.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="COM.YOU.YOURAPPNAME"
android:versionCode="1"
android:versionName="1.0" >
回答5:
You're importing invalid R class, import yourpackage.R class for example com.example.R
what actully happends that u import android.R class not yourpackages.R
回答6:
Ivwd simply removed line "import android.R;" and call Project->Clean... That was enough for my on Win-7/x64 and Eclipse Kepler and last (02.2014) Android SDK
回答7:
in 2019 I faced the same problem and I searched the internet and found the following which I am sharing with all.
In android studio letter R stands for the resources and this error occurs because of the build process not able to sync resources with your project. In other words, this error is caused when Android Studio can’t generate your R.java file correctly. This problem happens when you shift code to another system or while building the android project for the first time. So when you create a new activity or new class you will see an error message like “cannot resolve symbol r” with a red underline.
Below you can find the possible ways to fix cannot resolve symbol r in android studio.
Update Project Gradle To Latest Version Always use the latest version of Gradle to work android studio properly.
Sync Project With Gradle File Once you update the Gradle plugin you need to sync project with the Gradle file. Open android studio and click on Files > Sync Project with Gradle Files option.
Clean and Rebuild Project The most effective solution is the simplest: clean and rebuild your project. Select Build > Clean Project from the Android Studio toolbar, wait a few moments, and then build your project by selecting Build > Rebuild Project.
Invalidate Caches / Restart If you encounter this error after moving some files and directories around, then it’s possible that the R.layout error is being caused by a mismatch between Android Studio’s cache and your project’s current layout. If you suspect this may be the case, then select File > Invalidate Caches / Restart > Invalidate and Restart from Android Studio’s toolbar. Issues with the names of your resources can also prevent the R.java file from being created correctly, so check that you don't have multiple resources with the same name and that none of your file names contain invalid characters. Android Studio only supports lowercase a-z, 0-9, full stops and underscores, and a single invalid character can cause an R.layout error across your entire project, even if you don’t actually use this resource anywhere in your project!
My problem and its solution: In my case, I applied all the above but could not solve the problem. Thus I started a new project and pasted my code one by one and validated my code with running the app. Finally, at one point when I first deleted the code in colors.xml and copied and pasted code below in colors.xml file, I got the error.
<color name="bg_login">#26ae90</color>
<color name="bg_register">#2e3237</color>
<color name="bg_main">#428bca</color>
<color name="white">#ffffff</color>
<color name="input_login">#222222</color>
<color name="input_login_hint">#999999</color>
<color name="input_register">#888888</color>
<color name="input_register_bg">#3b4148</color>
<color name="input_register_hint">#5e6266</color>
<color name="btn_login">#26ae90</color>
<color name="btn_login_bg">#eceef1</color>
<color name="lbl_name">#333333</color>
<color name="btn_logut_bg">#ff6861</color>
when I undo my changes the error vanished again. Thus my code in colors.xml is not the code above and the code already in colors.xml i.e
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="bg_login">#26ae90</color>
<color name="bg_register">#2e3237</color>
<color name="bg_main">#428bca</color>
<color name="white">#ffffff</color>
<color name="input_login">#222222</color>
<color name="input_login_hint">#999999</color>
<color name="input_register">#888888</color>
<color name="input_register_bg">#3b4148</color>
<color name="input_register_hint">#5e6266</color>
<color name="btn_login">#26ae90</color>
<color name="btn_login_bg">#eceef1</color>
<color name="lbl_name">#333333</color>
<color name="btn_logut_bg">#ff6861</color>
Hope it will help someone like me who is new in programming.
回答8:
goto tools->android->Sync project with gradle file then click on S
回答9:
In my case , i accidentally imported my library's R. class not my app module's R. After editing this, it is resolved.
回答10:
There is probably an issue with the layout files, correct it and the auto generated class will appear.
回答11:
I have a few suggestions:
Make sure you don't have any other errors other than the R-related errors. Right-click your project folder in Eclipse, Android Tools -> Fix Project Properties.
2.Check to make sure you have the correct R imported. Sometimes the default Android.R can be imported.
3.Check for errors in your layout XML files.
回答12:
This solved my problem,
apt-get install ia32-libs
and restart :)
Android adb not found
回答13:
or you can write
import.R.layout // at the top
It will work for sure
回答14:
I used maven as build tool, after I build my project and dig a lot in the internet to solution. My problem resolved by adding to the main_activity.xml
tools:context=".MainActivity"
回答15:
I've got this when an inappropriate character (/-one slash) was added to the values/strings.xml, and, of course, renaming of problematic resource was helpful. And one more important thing - check the date and time on your device.
回答16:
JUST REMOVE the import.android.R;
Then the R.layout.(XML LAYOUTNAME) will turn to BLUE
IT TOOK ME HOURS TO FIND THIS ONE ERROR
回答17:
I had a similar problem.
It happened suddenly, after deleting some pictures from res. drawable.
I did two things.
- First I checked all my images in the drawable folder,if they all have preview. one of mine was damaged.
- Secondly, I closed all opened android studio and emulator. After that every things worked fine.
回答18:
Happened once. Don't know the reason.
Build> Clean Project removed the error for me.
回答19:
Either your gradle or build need to know where resources are.
回答20:
In Android Studio go to Tools
-> Android
-> Sync project with gradle files
or File
-> Sync project with gradle files
in latest Android Studio version.
回答21:
After cleaning the project and syncing with gradle(no result), i removed all my import statement, and brought them back in using alt+Enter.
回答22:
Thanks to @RubberDuck's comment and @Emil's answer I was able to figure out what the problem was. The IDs of most elements in my XML file were exactly the same. So, I renamed each and every one of them. Also, my XML file contained capital letters. The filename should be in [a-z0-9_] so I renamed my files too and the problem was solved.
回答23:
You're importing invalid R class, check imports.
回答24:
I removed import android.R and added import android.support.v7.appcompat.*;
However this had to be done after importing appcompat by right click project and import
来源:https://stackoverflow.com/questions/16045118/cannot-find-r-layout-activity-main