Reputation: 13
So, I am trying to do an application which gets a random number between 1 and 4 ( 1 inclusive and 4 exclusive ) and after, getting that number, it would change the background color of my Main Activity to the related number:
If it gets the number 1: Change to Blue If number 2: Change to Black If 3: Change to Yellow
Here's the code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
Button button2 = (Button) findViewById(R.id.button2);
button.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
Random rnd = new Random();
int random = rnd.nextInt(1 - 4);
if (random == 1) {
layout.setBackgroundColor(Color.BLUE);
} else if (random == 2) {
layout.setBackgroundColor(Color.BLACK);
} else {
layout.setBackgroundColor(Color.YELLOW);
}
}
}
);
}
The code seems fine to me and Android Studio doesn't report any errors ( only a warning because of the button2 ), but everytime I tap the button, the app just closes and says "Unfortunatly, Exercise has stopped."
My question is: Why does the application stop working after I tap the button?
( If any more information is needed, tell. I never asked and I am quite a novice in asking and Android Development )
Upvotes: 0
Views: 96
Reputation: 420
int random = rnd.nextInt(4);
OR
int random = rnd.nextInt(3);
Upvotes: 0
Reputation: 5592
The random.nextInt(int bound)
method accepts the bound parameter, this is that method in JDK:
public int nextInt(int bound) {
if (bound <= 0)
throw new IllegalArgumentException(BadBound);
int r = next(31);
int m = bound - 1;
if ((bound & m) == 0) // i.e., bound is a power of 2
r = (int)((bound * (long)r) >> 31);
else {
for (int u = r;
u - (r = u % bound) + m < 0;
u = next(31))
;
}
return r;
}
So, if the bound
is less or equal to 0 it throws exception, your bound is -3, thus resulting in that exception. Change your bound to random.nextInt(3) + 1
.
Upvotes: 0
Reputation: 61
As stated by Bathsheba, your logic for the nextInt function is incorrect.
As detailed in the Oracle documentation for Random and nextInt(int n), the nextInt function "Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)".
Therefore, you are trying to generate a number between 0 and -3, which causes an "IllegalArgumentException error", since 'n' is not positive.
One correct way to generate a random number between 1 (inclusive) and 4 (exclusive) is the following...
int random = rnd.nextInt(3) + 1
Upvotes: 0
Reputation: 83
int random = rnd.nextInt(1 - 4);
the problem is with above line, because it can throw an Exception.
Try to catch this Exception with try {} catch {} blocks and you will see.
Also you can change your algorithm for example use list with colors
List<Color> colors = Arrays.asList(Color.BLUE, Color.RED, Color.YELLOW);
layout.setBackgroundColor(colors.get(rand.nextInt(colors.size() - 1));
Upvotes: 0
Reputation: 223
Declare this: LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
Out side of your onClick listener.
Also Try this for your random number generator between 1 and 4
Random r = new Random();
int i1 = r.nextInt((4 - 1) + 1) + 1;
Upvotes: 0
Reputation: 146
Check your logcat for more information.
This line seems strange to me: rnd.nextInt(1 - 4);
Is it right?
Upvotes: 0
Reputation: 234655
rnd.nextInt(1 - 4);
evaluates to rnd.nextInt(-3);
and that will throw an IllegalArgumentException
due to a negative argument.
As you are not handling that exception yourself, bad things happen to your application. To generate random integers in a range, see How do I generate random integers within a specific range in Java?
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Random.html#nextInt(int)
Upvotes: 1