R cannot be resolved to a variable [duplicate] - java

This question already has answers here:
"R cannot be resolved to a variable"? [duplicate]
(30 answers)
Closed 2 years ago.
I got all of this code from here
I am extremely new to all of this, and am having a few problems.
The error "R cannot be resolved to a variable" comes up on the bold parts of this code.
Resources r = context.getResources();
**setBackgroundColor(r.getColor(R.color.candidate_background));
mColorNormal = r.getColor(R.color.candidate_normal);
mColorRecommended = r.getColor(R.color.candidate_recommended);
mColorOther = r.getColor(R.color.candidate_other);
mVerticalPadding = r.getDimensionPixelSize(R.dimen.candidate_vertical_padding);**
mPaint = new Paint();
mPaint.setColor(mColorNormal);
mPaint.setAntiAlias(true);
**mPaint.setTextSize(r.getDimensionPixelSize(R.dimen.candidate_font_height));**
mPaint.setStrokeWidth(0);
The only thing I can think of is that I haven't copied and pasted everything correctly?

R is a Java class file that basically maintains an index of resources by assigning every resource that your application uses with an integer value for lookup. If you look in the res directory of the sample you're following, you'll see a values directory underneath it containing a colors.xml file that defines any custom colors. Note that in this specific example, you are using a color resource, not a Color State List resource as Paul had mentioned. Color state resources go in the color directory, but definitions of simple colors go in the values directory. Confusing, I know.
Resources in Android are defined via XML files. R.java is created automatically by the Android build process when these XML files are parsed. Different types of resources need to be placed in to specific directories for R to be structured properly; quite unhelpfully, not all of these directories are automatically created for you when you make a new project under an IDE.
Read this article about Application Resources, this article about providing resources, and this article about accessing resources (all Google documentation) for a bit of a better understanding.
Lastly, if you DO have all of your XML files in the proper place, check your import statements. Sometimes conflicts can arise if you have too many imports or if all of your imports aren't in place; if you're using the official Android Dev Tools in the form of the Eclipse plugin, the easiest way to handle imports is to use the Ctrl+Shift+O shortcut (Cmd+Shift+O on a Mac). If your imports are correct, and all of your XML files are in place, perform a rebuild on the project. If this still doesn't work and you are using Eclipse, I know that sometimes Resource resolution conflicts can inexplicably be fixed by simply closing Eclipse and launching it again.

R is an automatically generated file made from your resources.
Taken from Android docs
Color State List Resource
Define a color resources that changes
based on the View state. Saved
in res/color/ and accessed from the R.color class.
I would guess that the different R.color.candidate_* all need to be defined under res/color/

The R.java file should be automatically generated by Android. Try the following:
If you are using Eclipse try "project clean" to regenerate the file.
Use Ctrl-Shift-O to "reorganize" your imports. This will automatically add the R.java file to your imports if necessary.
Try to fix all errors not related to the R file and then retry the "project clean" and Ctrl-Shift-O option again. Other errors (e.g. typos in your xml layout files) can "stall" a new build of the R file via project clean.
Make sure your project is an Android project and you have an android.jar file on your classpath. If not fix this and retry step 1-3

you ned to copy the resources om the example you are using into the releavent directories in you res folder. The first word (after R.) is the directory you use.
so for R.color you put the resoruce in res/color in your project.
for the xample the resources are here:
http://developer.android.com/resources/samples/SoftKeyboard/res/index.html
they should also be in the version of the android SDK you downloaded:
ADKPATH/samples/android-/SoftKeyboard/

I got this error message many times. And mostly all of them were caused by some mistake in xml files or that I forgot to put 'check' in front of Build Automatically option. Try it! Maybe it will help You.

Related

tuProlog - using multiple files with consult - can't get engine to load additional files with consult(otherFile.pl)

Edit: Clarity - the main .pl file loads, it's all the subfiles that it has been told to load which don't load. (all the consult('subfile.pl').)
I have a Java project that uses tuProlog. It calls a theory as:
Theory theory = new Theory(":-consult('main.pl').");
engine.setTheory(theory);
This is as per the manual.
file.pl exists in the same folder as other prolog files.
Inside of main.pl, I have further
consult('otherfile.pl').
statements to load additional files (several).
The folder structure is:
src/main.pl
src/Prolog_Files/otherfile.pl (multiple)
src/main/java/JavaStuff
I can't get the engine to load the theories that I've told it to consult inside of the main file.pl
I have tried: giving it absolute path instead of just filename.
moving files around.
I'm wondering if there is something about usage of tuProlog I'm not understanding?
The theory works when loaded with:
Theory theory = new Theory(new FileInputStream(url_of_file)).
However, this is causing me issues when building the jar, as it can't find the file location.
Am I attempting to load the file correctly? Are my consults inside of the main .pl file correct?
Could someone please post an example of how this should be done if not? The manual doesn't elaborate very much on this topic.
Thanks
The manual is slightly outdated in parts - it says to use consult/1, whereas elsewhere it states that consult/1 is deprecated, whereas include/1 is the replacement.
Secondly, when using 2p.jar, it reads Prolog files from the Project root as its root. When creating a jar, 2p.jar cannot be inside of the project jar. They should be in relative folders, and 2p.jar reads Prolog files with the location of 2p.jar as root. It doesn't seem that it can read inside of project jar.
Hopefully that's clear enough!

Cleaning messed up my Android project

I changed some images in drawable-xxhdpi folder from jpg to png (by deleting jpg and copying png). When I started Eclipse, the error reported was:
res\drawable-xxhdpi-v4\stripes.png:0: error: Resource entry stripes is
already defined. res\drawable-xxhdpi-v4\stripes.jpg:0: Originally
defined here.
So I look that up and it says I should do Project>Clean, so I do, and now all .java files are are reporting errors on every line where R.something is used, for example:
setContentView(R.layout.activity_about);
reports error R cannot be resolved to a variable.
Then I looked this up and the problem is said to be xml errors, but:
I didn't touch any of the xml files, I just changed jpgs to pngs
none or the xml files have errors reported anywhere
You can't have two resources of the same type with the same base name. You have these two right now:
res\drawable-xxhdpi-v4\stripes.png
res\drawable-xxhdpi-v4\stripes.jpg
They have the same base name "stripes" but with different extensions.
When the Android tools build your app, the name of the resource in your R class only contains the base name of your resource, but it will complain if two resources have the same base name.
You just need to give one of them a different base name so they can have two different resources defined.

eclipse java can not find xml file

i am currently having a problem regarding an xml file while programming in eclipse android.
the problem is that it seems to ask for a folder called R within my project but such a folder was not created on creation of the project.
i have had a look on the internet and review other questions but i cant quite seems to find a solution.
i am trying to set an xml file for a list and this is all the code that should be needed but it can not find the .xml file at all as it seems to want a folder called R to contain it.
new ArrayAdapter<Object>(this,R.layout.edit_item_layout , theList);
anyhelp and suggestion will be well received
UPDATE of useful information
The weird part is it can find activity_main.xml which is in the same folder
the file is located in res/layout
ok i cleaned and now it is giving a list of files i can change it to but the file itself doesn't seem to be in the R.java file ad it is asking me to change it to different files but not the one i want
R is automatically generated, and you should never add or delete from this file.
I have faced this problem many times, and it mostly caused by xml file.
You should check your XML file carefully. It is most likely that you will find your answer there.
If your importing any library, make sure you're importing those file correctly.
R refers your res folder. You should have a subfolder in res called layout that contains your edit_item_layout.xml.
If you're receiving the error R cannot be resolved to a variable, you'll need to:
import your_package_name.R;
If you're receiving the error edit_item_layout cannot be resolved or is not a field, try:
Cleaning your project by selecting: Project --> Clean
Right clicking your project and selecting: Refresh
If you're R.java file is corrupted, because it's automatically generated you can also delete your gen folder and it should be replaced immediately.

assetManager.loadModel results in AssetNotFound Exception

The model location has been added to the class path. I also tried importing the model which placed it in a Users folder in the project. The .loadModel statement works in jmonkey but not in java:
rotor1 = assetManager.loadModel("Models/Rotor2/Rotor2.mesh.j3o");
I've tried several alternatives to ("Models/Rotor2/Rotor2.mesh.j3o")
("C:/Users/Eugene/Documents/JMonkey/BasicGame1/assets/Models/Rotor2")
("Rotor2.mesh.j3o")
("Users/Rotor2.mesh.j3o)
The actual exception is shown as:
Uncaught exception thrown in thread[LWJGL Renderer Thread,5,main]
AssetNotFoundException: Model/Rotor2/Rotor2.mesh.j3o
I've also tried other variations of the .loadModel statement, with the same error.
I could really use some help with this.
When you load a model it uses the jME3 asset system to do so. If you use the SDK to create your projects then it will create an assets folder for you and you can place the assets within that. The assets are actually embedded within the JAR of your program.
If you want to load assets from another location such as a folder on the local hard drive you will need to register your own asset locator. You can find this described here:
http://hub.jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_asset#loading_assets_from_custom_paths

Concept behind the Java resources, striking gs, "R.id"

I ran into an issue that made me realize there is core concept I'm not getting.
For some reason when I went to add a value to my strings.xml file, something happened and my xml file was giving a parsing error. The only way I was able to fix this was by deleting the strings file and re-creating it by right clicking on the values folder, and going to "add android xml file" and naming it strings.xml.
This made the error go away, but now, my activities are complaining and saying they can't resolve R (as in R.id).
This made curious as to what's the relationship between R and the xml files. And why is that when you reference a string, you use #String/something, when the file that contains it is called strings?
Finally why is it that even though I created an xml file called strings.xml, in the right location, my project doesn't see his as the THE strings.xml file?
Names
The R resources are organized in a specific manner:
R.layout.* - These resources are your layout files, which you have created in the various layout-* folders
R.menu.* - These are the resources which are your menu files, created in your various menu-* folders.
R.id.* - These are the resource identifiers for your various Views and Menu items etc. There can be more than one element with the same ID, as long as they're in separate files.
R.string.* - These are your string resources, declared in <string> elements in files in your values-* folders. The filename is irrelevant. There can only be one string element with a particular name in a single folder. So you cannot have two hello strings in values, but you can have one each in values and values-en.
R.drawable.* - These are the various XML and image drawables you have in your drawable-* folders.
There are many more, but these are the most common. I'll add the rest when I have a bit more time on my hands.
After compilation
During compilation, the Android build system puts all XML files into a binary format. Everything except the /res/raw is compressed as well. This is particularly helpful with layouts, which can be inflated faster after compression. Individual files cease to exist after compilation, and everything becomes one big binary file. You access the data in this file using R.<resource_type>.<name> from Java, and #<resource_type>/<name> from XML
Naming
For some resources (drawables, layouts, menus) the filename matters, as the resource is referred to using that. For other resources (strings, colors, styles), the filename is irrelevant, as long as you use the correct element (for example <string> for Strings) when declaring them within the file.
When your project gets compiled and built, one of the things that happens is the generation of the R file. Each named resource is given a public static final int identifier, like R.string.foo, which would reference a string defined with name="foo". This way you can reference a resource from Java and Android will map it to a resource and load it from your application package.
Resources can be overloaded; you could have two strings both named foo that are qualified for different configurations, but they have the same identifier in the R class. The system will choose the one that best matches the device configuration at run time.
If there is an error with any of your resource declarations, R will fail to build and any java classes that reference it will therefore complain about it. You can do Project > Clean in Eclipse and see if that helps, but otherwise make sure your resources are properly declared and/or have valid names.
R.java is a seperate file created by eclipse,
its not related to strings.xml ,Clean your project then build and run again .
you will found R.java inside your gen folder
Read this
http://developer.android.com/tools/projects/index.html

Categories