I have a problem changing the background color randomly:
First, I try to use parseColor In the Colorclass:
public class Colors {
public String[] colors = {
"#39add1", // light blue
"#3079ab", // dark blue
"#c25975" // mauve
};
public int getcolor() {
Random randomGenerator = new Random();
String color = "";
int randomNumber = randomGenerator.nextInt(3);
color = colors[randomNumber];
int colorAsInt = Color.parseColor(color);
return colorAsInt;
}
}
And in the Activity class:
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
int color = mColors.getcolor();
relativeLayout.setBackgroundColor(color);
}
};
It works perfectly:
But when I try to use String for return type of getcolor and use parse int in the activity class, when i run the app it gives me an error :Unfortunately app has stopped.
The color class:
public String getcolor() {
Random randomGenerator = new Random();
String color = "";
int randomNumber = randomGenerator.nextInt(3);
color = colors[randomNumber];
return color;
}
And the activity class:
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View view) {
String color = mColors.getcolor();
relativeLayout.setBackgroundColor(Integer.parseInt(color));
}
};
Why this problem happens?