R Cannot be resolved in a freshly created project - android

I'm completely new to Android programming and there's this thing bugging me already. I get the R cannot be resolved error in a freshly created project! Because it's freshly created, I didn't edit it at all, so I'm sure it has no errors and it should just be a hello world program.
I checked and there's no
import android.R;
there and I have no idea what else could be causing this. Because of this error I can't even attempt practice in making layouts...

Try changing the project's API level, apply the changes and then revert to the old API level, then clean your project. I'm not sure how, but this worked for me sometimes.

To clean android project....
Window -> Preferences -> General -> Keys (**Ctrl+Shift+L**)
in type filter text write build clean.
here you can bind the key. for ex Ctrl+Shift+L

Another solution. I changed the target sdk to 2.1 after creating the project. Android showed no errors in the layout, so I restarted eclipse (cleaning didn't help) and then Android marked the layout with a red x. It didn't recognize the match_parent parameter since it was introduced in Android 2.2. The solution is to change the target sdk to 2.2 or use fill_parent or wrap_content instead of match_parent.

Related

Intellij android resources gen

I'm experiencing a strange behaviour from Intellij that I never had before. I don't know if it's related with this new update (14.0.3) or what. So what happens is every time I add a new resource, let's say a new .xml or a new View in a previous .xml file, my R file doesn't get updated, I've to compile the hole project again, otherwise it warns me about not finding that resource. I've tried to delete the gen folder but it doesn't work either and it gets worse, all my R references get mixed up so I end up with CastExceptions and misplaced drawables, the only was is to re-compile again... It never happen to me before.
Anyone experienced this kind of behaviour?
Four Suggestions:
1) Make sure that you are indeed using the latest-greatest Android SDK Build Tools.
2) You could probably use lint to determine the lint warnings. Go to File > Settings > and search for lint in the search box. You will find lot of different options in there. Make sure they are checked. I have had some instances when lint would complain that i am using an attribute in the .xml that is not supported and that sometimes catches Intellij off guard.
3) Go into the project structure and at the very bottom there should be a tab Problems. Make sure that tab is clean. Sometimes if there is an issue with the path while using third party libraries, R.java breaks.
4) Make sure you are indeed using the correct api's available for the target/min SDK you specify.
Hope this helps. If you find the solution please update the OP so that it might be helpful to someone else.
So I've contacted JetBrains support and their response was:
Andrey Dernov (JetBrains Support)
Feb 16, 11:41
Hello,
There was a bug with Android projects https://youtrack.jetbrains.com/issue/IDEA-135234 . The issue will be fixed in upcoming 14.0.4 update. Or you can use the last EAP build from https://confluence.jetbrains.com/display/IDEADEV/IDEA+14.1+EAP . We are sorry for inconvenience.
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

Android Studio issue with R file

I was using Android studio now for a long time and it was fine. Recently, not sure what happened, but when I click on debug and have it perform make it complains about the R file and it seems like it doesn't generate it.
The only way it works is if I click on Build > Rebuild Project...problem is that it takes a long time for that to go through.
Is there anything I am missing? I updated the Android SDK tools, etc. as per some of the other suggestions?
Yes this is a known issue in the newest version of android studio. Google has suggested a temporary workaround until they can track down the problem and fix in next release. Turn off external builds in preferences > compiler. It might help.
Source: cannot resolve R into variable after Android studio update and R.java not updated after update to 0.1.6 .
Hope this helps.
Solution for android studio Users :
Goto : Menu tab - > Build - > Rebuild Project
after rebuild Project.R.Java file will be solve automatically ..
I was having the same problem, but it looks like a new update came out today. It seems to have solved the problem.
Try to import android.R, for every class you need R (of the package)
and then compile, it makes different error from before.
And remove all ('import android.R') , and rebuild it.
This trick is not for solving problem in proper way,
but makes the tool refresh.
And remove all "import android.R" , and rebuild it and import them again its working for me

Android studio suddenly cannot resolve symbol R

I was having a project open in Android studio. It was generated by the wizard and working fine.
I did some small changes to activity_main.xml and when I changed back to MainActivity.java i get the error in several places that it cannot resolve R. I might have done something to cause this. But I´m not sure what since it appered when I edited the xml.
Does anyone know what might be the solution to this?
I can find the R.java in r/release/packege/com/ and it looks fine.
Most of the time it is due to a bad XML file. XML files can be layout files, value files, or the Manifest file. Please check your xml files and try to rebuild the project. Sometimes cleaning the project and rebuilding it also works.
In addition, make sure you do not have a drawable with an invalid name. I had a drawable with a numeric filename and that didn't sit well with Android so it failed to compile R.java.
Downgrade Your Gradle Plugin Version
No amount of cleaning, rebuilding and restarting would do the trick for me.
The only thing that did the trick was downgrade our Gradle version from 3.4.0-alpha02 to 3.2.1.
So, instead of:
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0-alpha02'
}
We used:
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
}
After making that change and then doing a Gradle sync, everything worked.
Not sure if it's related to the alpha release or to that version or if changing your Gradle version at all just forces a refresh that otherwise cannot be done with the other commands, but there ya go.
Make sure in your AndroidManifest.xml the package name is correct. That fixed the problem for me when my R.whatever was marked red!
Try to clean the project by doing the following:
If you are using Windows, open CMD, cd to your project directory and run gradlew clean.
I just had this problem and the reason was that after editing the layout android studio automatically added an import to the source files:
import android.R
Of course the main R class has not all the symbols in your layouts, which made everything a red mess.
Removing that line solved for me.
EDIT:
Seems like eclipse had the same problem early on.
I sometimes get this after updating Android Studio. Not always straight away but after a while (perhaps after a restart of the program).
Here is what has worked for me: Tools - Android - Sync project with Gradle files.
For some reason just clicking the "Sync now" link that shows up after changing the build.gradle file has not done any change for me. Even though the messages says approximately the same thing.
I had the same problem because in my strings.xml I forgot to escape a apostrophe. Use " \' " never " ' " in xml file!
I had that same problem crop up, too (most recently while I was at lunch--came back and WTF?). And since this was a simple Hello, World program, there wasn't many places these errors could hide. The XML files looked fine--I hadn't even touched the manifest. A clean did nothing.
So I commented out a section of my layout.xml to try to narrow down the problem. Sure enough, the errors went away.
And then I simply UNcommented out exactly what I had commented...and everything was hunky-dory. Strange! Perhaps there was some lingering issues that needed to be touched in order to cause a rebuild of the layout file. You'd think basic stuff like that wouldn't happen these days, but they do.
One more thing! It's possible that something may happen to Java as well. It's very hard to know the status of the Java run-time environment; just easier to start again. So here's one more thing to try: restart your computer, restart Android Studio, AND THEN Invalidate Caches and Restart. Yep, the whole-hog.
When all else fails restarting android sometimes helps. During the start files are loaded afresh and this enables detection of the R
This problem occurs when you rename the package name.
After renaming the package name, you must change the older package
name to a newer one.
For me it solved the problem once I changed that.
After that, you might get errors in xml files, that you have to change
the package name there also.
That's it.
I just had the R problem and after a few hours found that the problem was my regional spelling as used in one of my xml files. The problem word was behaviour, which is spelled behavior by Americans.
I am Australian and we are taught ye olde English way of spelling certain words and so I looked past it a number of times before the penny dropped.
Its a small thing, but it can easily sneak up and bite you on the nether regions and is hard to spot and can waste a lot of time.
In my case I had just added some new image files to the drawable folder and accidentally put a GIF in there with my PNGs. I had not incorporated them into the project at all yet. I removed the GIF and then did a clean and rebuild. That fixed it for me.
I had this exact problem when importing a Udacity Example Program. The Message Pane in Android Studio said the error was failing to load a .PNG in a sub-directory deep in the Project Files.
Turns out the PNG with all of the sub-directory path names was greater than the name length limit defined by Windows 7 (260 characters). I gave the Project a shorter name - to free up some space - and then Android Studio happily compiled.
After you tried make project, clean project, rebuild project but still doesn't work.
Try restart Android studio

ActionBarSherlock and new Android SDK

I had an existing project working with ActionBarSherlock and the android SDK (tools version 19 i think) i upgraded the Android SDK to the latest version (21) and suddenly all the R variable stuff is missing, example: R.layout.fragment_account_layout it claims does not exist and needs to be created. pretty much everything related to the R variable is messed up. i have tried a couple different things to fix it; cleaned the project, cleared out the workspace and re-added everything. nothing has worked. how i do resolve this issue? is it a change i need to make to my configuration?
You may need to check that your activities are not importing the wrong R file ( In this case it may be importing the R from the ActionBarSherlock instead of your application)
Worst case you may need to add manually your R into your activities.
like
import your.app.namespace.R;
within the class and delete any other R being imported

What can I do with R cannot be resolved problem?

If I make a new Android project in Ecplipse and I try, to open one of the samples from android SDK, I have got an error, look like this:
R cannot be resolved
What can I do, to be able, to run these files? I tried to delete import android.R; but nothing changed.
Thanks
First thing you might try is "clean" your project (option in the menu). If it persists:
There should be an error that stops R from being generated. Check the "problems" tab (?) in eclipse to see what that problem is, and solve that. It could be missing libs, or various other stuff.
You possibly got error in building resources.
Possible fixes:
-refresh your project (F5)
-clean you project and refresh
-see "Console" tab, sometimes the problem is shown there
-reload the Android SDK
-restart the Eclipse IDE
Check your XML-Layout files. The problem come here half a time.
If you are sure it is note your layout file, check in your java build path if the google librarys are well set. (Google APIs)
(sorry for my bad english)

Categories

Resources