iqueqiorio
iqueqiorio

Reputation: 1187

Android - Compile error variable cannot be applied

If have this enum type code and get this compile error.

enter image description here

I get that Color.RED is an int but Im not sure why I am getting the error?

 enum Direction {

    NORTH(Color.RED), WEST(Color.BLUE), EAST(Color.BLACK), SOUTH(
            Color.GREEN);

    Direction(Color c) {
        color = c;
    }

    private Color color;

    public static Direction fromName(String name) {
        for (Direction d : Direction.values()) {
            if (d.name().equalsIgnoreCase(name)) {
                return d;
            }
        }
        return NORTH;
    }

    public Color getColor() {
        return color;

} }

Thanks for the help in advance.

Upvotes: 0

Views: 137

Answers (2)

aadi53
aadi53

Reputation: 449

public enum Direction {

NORTH(Color.RED), WEST(Color.BLUE), EAST(Color.BLACK), SOUTH(
        Color.GREEN);

Direction(int c) {
    color = c;
}
private int color;


public int get() {
    return color;
} 
}

and to get color in int format use :

Direction.SOUTH.get();

which will return int value for color "Green"

Upvotes: 3

Thorben
Thorben

Reputation: 1029

In your constructor you declare that the parameter has to be an instance of the class Color. Color.RED for instance however is returning an Integer.

So you would either

  1. have to replace Color.RED and so on with objects of Color

OR

  1. change your enum to store an int.

Upvotes: 2

Related Questions