开发者

R cannot be resolved to a variable [duplicate]

开发者 https://www.devze.com 2023-03-18 10:39 出处:网络
This question already has answers here: "R cannot be resolved to a variable"? [duplicate] (30 answers)
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);*开发者_C百科*

    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:

  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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消