Android : parse color VS parse int

on Sunday, August 31, 2014


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?


0 comments:

Post a Comment