views:

59

answers:

2

I don't understand why Eclipse doesn't know how to resolve the color resources I've defined. Am I doing something wrong?

R.color.notepad_lines cannot be resolved MyNewTextView.java

I've had problems before too with eclipse being able to find images in my /res/drawable directory

/res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <color name="notepad_paper">#AAFFFF99</color>
 <color name="notepad_lines">#FF0000FF</color>
 <color name="notepad_margin">#90FF0000</color>
 <color name="notepad_text">#AA0000FF</color>
</resources>

MyNewTextView.java

...
 private Paint marginPaint;
 private Paint linePaint;
 private int paperColor;
 private float margin; 


 private void init(){
  //Get Reference to Resource Table
  Resources myRes = getResources();

  //Create paint brushes
  marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  marginPaint.setColor(myRes.getColor(R.color.notepad_margin));

  linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
  linePaint.setColor(myRes.getColor(R.color.notepad_lines));

  paperColor = myRes.getColor(R.color.notepad_paper);
  margin = myRes.getDimension(R.dimen.notepad_margin);
 }

...

Eclipse keeps saying it can't find any of the R.* resources =/

R.java

public final class R {
    public static final class attr {
    }

public static final class color {
    public static final int notepad_lines=0x7f040001;
    public static final int notepad_margin=0x7f040002;
    public static final int notepad_paper=0x7f040000;
    public static final int notepad_text=0x7f040003;
}
public static final class dimen {
    public static final int notepad_margin=0x7f050000;
}
public static final class drawable {
    public static final int ic_menu_add=0x7f020000;
    public static final int ic_menu_cut=0x7f020001;
    public static final int ic_menu_king=0x7f020002;
    public static final int icon=0x7f020003;
}
...
+1  A: 

I've had this problem too, unless I let Eclipse create the file, rather that drop the XML file in under res/ somewhere. But I just found a way around this: In the Project Explorer view, find the res/values directory and select refresh from the right click menu: alt text

colors.xml now appears in the explorer, and the R.java now contains the R.color class. Rebuild it you don't auto-rebuild.

Frayser
Did you just make that right now, or are you doing the same example that I am? ^_^ Unfortunately refresh did not solve the issue for me
codeninja
I just tried to solve this because I was having the same problem when I tried to add pre-existing files. I'm not on the same project. Eclipse seems to ignore new files by default. I created a file in XEmacs with your xml and saved it as res/colors.xml. I added`int `line_color=R.color.notepad_lines;` in the main `Activity` class which is just a clone of my HelloWorld project. I could not build, because gen/.../R.java did not define the `color` class. After refresh, the R.java got updated and the project built find.
Frayser
Yeah it seems to only "fix" when I hit Run As > Android App, and no other time =/
codeninja
You shouldn't have to do that: In my case, my package is package org.frayser.practice; R.java defines class color as org.frayser.practice.R.colors. <- Those are my pre-defined colors from colors.xml. `android.R.color` is another `R.color` class that has a different set of pre-defined colors. [See the doc](http://developer.android.com/reference/android/R.color.html), then compare to your *R.java.*
Frayser
For this to work, there need to be a *gen/your/package/path/R.java* file. The *R.java* should have `package your.package` declared. Inside the file should be `class R` with a inner `class color` with some `int` values defined. My original problem was that the R.java never got updated with `class color`, plus *colors.xml* didn't even show up in the *Project Explorer*. Seems you have a *R.java.* with `color`s in it. Check the package names.
Frayser
OMG! I had "import android.R" which didnt seem like the right path, but taking that out seemed to fix it =/
codeninja
`android.R.colors` was shadowing your`R.colors` Glad you got it fixed. It's weird how Android uses the same class name rather than a different one, or subclassing.
Frayser
shadowing? what does that mean
codeninja
Two packages, classes, namespaces etc export the same name. When you look for the name you get the wrong one because you didn't know there were two, or you mistakenly wrote the wrong code. `Point(x,y){ this.x = x; this.y=y; }` Used this.x because there were two x's one shadowed the other. See 6.3.1. [Shadowing Declarations]:(http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#34133)
Frayser
There is a [standard R.color class](http://developer.android.com/reference/android/R.color.html). There is an entire [android.R](http://developer.android.com/reference/android/R.html). So if you `import android.R`, you *shadow* yow own `R.color`, and `R.*`, _from mypackage.R.java_. Compare the [standard R.color](http://developer.android.com/reference/android/R.color.htm) to your own R.color, and see the difference in color names.
Frayser
+1  A: 

This may be a stupid suggestion but... Is MyNewTextView.java in the same package name as the R.java file? If not, then you need to add a reference to the package via an import statement.

Eli
No, good suggestion. The package name isn't shown on any of the sample sources.
Frayser
Yeah it was a good suggestion this exactly was the problem. The package path for R was different than where R.java was generating.
codeninja