25 Feb 2012, 15:49
Generic-user-small

Will Andrews (2 posts)

Hey guys. I’m currently on section 4 and trying to set the colors in the puzzleview.java class.

Here is the code I have copied from the book.

@Override protected void onDraw(Canvas canvas){ //draw the background Paint background=new Paint(); background.setColor(getResources().getColor( R.colors.puzzle_background)); canvas.drawRect(0,0,getWidth(),getHeight(), background);

For some reason it’s throwing up an error when trying to reference the color.

I have checked the colors.xml file and it matches up.

What am I doing wrong?

29 Feb 2012, 18:39
Generic-user-small

Brandon Meglathery (5 posts)

Hey Will,

I believe your problem is in your line: background.setColor(getResources().getColor(R.colors.puzzle_background));

If you look closely, where you define your parameter “R.colors.puzzle_background”, color should not be plural, because in your .xml file you are defining it as a “color” not “colors”.

I hope that helps, if not I’m sorry! Let me know if it does :)

~Brandon

01 Mar 2012, 00:34
Burnette_ed_pragsmall

Ed Burnette (1316 posts)

Brandon is right. Although you define the color in “colors.xml” (plural) you reference it in code as “R.color.something” (singular).

01 Mar 2012, 18:58
Generic-user-small

Will Andrews (2 posts)

Cheers guys. That was exactly what it was. I keep spelling it colour as well :(

13 Apr 2013, 11:02
Generic-user-small

Adam Jasinski (2 posts)

Hi there. Im am currently stuck at section 4.3 of the Sudoku application and have kind of another problem here. I get 8 following errors:

“Avoid object allocations during draw/layout operations(preallocate and reuse instead)” for Paint background = new Paint();

“puzzle_background cannot be resolved or is not a field” for background.setColor(getResources().getColor(R.color.puzzle_background)); canvas.drawRect(0, 0, getWidth(), getHeight(), background);

“Avoid object allocations during draw/layout operations(preallocate and reuse instead)” for Paint dark = new Paint();

“puzzle_dark cannot be resolved or is not a field” for dark.setColor(getResources().getColor(R.color.puzzle_dark));

and the same errors for hilite and light as well.

That’s how whole that section looks in my PuzzleView.java

`protected void onDraw(Canvas canvas) { Paint background = new Paint(); background.setColor(getResources().getColor(R.color.puzzle_background)); canvas.drawRect(0, 0, getWidth(), getHeight(), background);

Paint dark = new Paint();
dark.setColor(getResources().getColor(R.color.puzzle_dark));

Paint hilite = new Paint();
hilite.setColor(getResources().getColor(R.color.puzzle_hilite));

Paint light = new Paint();
light.setColor(getResources().getColor(R.color.puzzle_light));

for (int i = 0; i < 9; i++) {
	canvas.drawLine(0, i * height, getWidth(), i * height, dark);
	
	canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1, hilite);
	
	canvas.drawLine(i * width, 0, i * width, getHeight(), dark);

	canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(), hilite);
}`
14 Apr 2013, 03:04
Burnette_ed_pragsmall

Ed Burnette (1316 posts)

The ‘avoid object allocations’ messages are just warnings. You can avoid them unless you are having performance problems.

R.color.puzzle_background is a reference to a line in your Sudoku/res/values/colors.xml file. It should look something like this by the time you’re done:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="background">#3500ffff</color>
   <color name="puzzle_background">#ffe6f0ff</color>
   <color name="puzzle_hilite">#ffffffff</color>
   <color name="puzzle_light">#64c6d4ef</color>
   <color name="puzzle_dark">#6456648f</color>
   <color name="puzzle_foreground">#ff000000</color>
   <color name="puzzle_hint_0">#64ff0000</color>
   <color name="puzzle_hint_1">#6400ff80</color>
   <color name="puzzle_hint_2">#2000ff80</color>
   <color name="puzzle_selected">#64ff8000</color>
</resources>

If you have those lines, make sure that your Java file does not contain the line “import android.R;”. If it does, delete it.

  You must be logged in to comment