1

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?

Blachshma
  • 17,097
  • 4
  • 58
  • 72
Elsa
  • 11
  • 1
  • 2
  • 1
    It's probably not an Android project or you didn't build it... R is a "generated" source file... it gets created automatically. – mibollma Jul 06 '11 at 02:10
  • No, I didn't build it. I got it off the android developers website. – Elsa Jul 06 '11 at 02:49

5 Answers5

2

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.

Doug Stephen
  • 7,181
  • 1
  • 38
  • 46
1

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/

Paul.s
  • 38,494
  • 5
  • 70
  • 88
  • 1
    In this specific example, the color definitions are actually in res/value instead of res/color because custom colors that don't change state aren't considered to be a "color state resource list". Google themselves recommend that "simple" definitions like colors go in to values and that the color directory should only be used for Color State Lists. Which I think is silly. – Doug Stephen Jul 06 '11 at 03:01
1

The R.java file should be automatically generated by Android. Try the following:

  1. If you are using Eclipse try "project clean" to regenerate the file.
  2. Use Ctrl-Shift-O to "reorganize" your imports. This will automatically add the R.java file to your imports if necessary.
  3. 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.
  4. 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
THelper
  • 15,333
  • 6
  • 64
  • 104
0

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/

siliconeagle
  • 7,379
  • 3
  • 29
  • 41
0

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.

lomza
  • 9,412
  • 15
  • 70
  • 85