Reputation: 541
So, I have looked at other places for how to generate a random number, and put it into an Android app. I would expect this to work, but sadly, it crashes with this LogCat:
11-03 17:06:48.930: W/dalvikvm(8327): threadid=1: thread exiting with uncaught exception (group=0x41ff0300)
11-03 17:06:48.938: E/AndroidRuntime(8327): FATAL EXCEPTION: main
11-03 17:06:48.938: E/AndroidRuntime(8327): java.lang.IllegalArgumentException
11-03 17:06:48.938: E/AndroidRuntime(8327): at java.util.Random.nextInt(Random.java:187)
11-03 17:06:48.938: E/AndroidRuntime(8327): at com.spng453.randomnum.RandomNumber$1.onClick(RandomNumber.java:24)
11-03 17:06:48.938: E/AndroidRuntime(8327): at android.view.View.performClick(View.java:4084)
11-03 17:06:48.938: E/AndroidRuntime(8327): at android.view.View$PerformClick.run(View.java:16966)
11-03 17:06:48.938: E/AndroidRuntime(8327): at android.os.Handler.handleCallback(Handler.java:615)
11-03 17:06:48.938: E/AndroidRuntime(8327): at android.os.Handler.dispatchMessage(Handler.java:92)
11-03 17:06:48.938: E/AndroidRuntime(8327): at android.os.Looper.loop(Looper.java:137)
11-03 17:06:48.938: E/AndroidRuntime(8327): at android.app.ActivityThread.main(ActivityThread.java:4745)
11-03 17:06:48.938: E/AndroidRuntime(8327): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 17:06:48.938: E/AndroidRuntime(8327): at java.lang.reflect.Method.invoke(Method.java:511)
11-03 17:06:48.938: E/AndroidRuntime(8327): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-03 17:06:48.938: E/AndroidRuntime(8327): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-03 17:06:48.938: E/AndroidRuntime(8327): at dalvik.system.NativeStart.main(Native Method)
11-03 17:06:51.376: I/Process(8327): Sending signal. PID: 8327 SIG: 9
I have tried everything I can think of to get this to work, but nothing does. Here's the code:
private OnClickListener Gen = new OnClickListener() {
public void onClick(View v) {
Random rand;
rand = new Random();
EditText maxin = (EditText)findViewById(R.id.max);
EditText minin = (EditText)findViewById(R.id.min);
TextView out = (TextView)findViewById(R.id.out);
int max = Integer.parseInt(maxin.getText().toString().trim());
int min = Integer.parseInt(minin.getText().toString().trim());
out.setText(Integer.toString(rand.nextInt(max-min) + min));
}
};
Upvotes: 4
Views: 1731
Reputation: 2897
java.lang.IllegalArgumentException
indicates that you're passing an illegal argument to you nextInt()
function. I'm guessing that your argument is less zero, or something similar to that. Check those values in the debugger to make sure your arguments are valid.
Upvotes: 1
Reputation: 1502176
Well, that will certainly happen if your max
text box has a number smaller than or equal to your min
text box. You should validate that before calling Random.nextInt(int)
. As the docs state:
Throws:
IllegalArgumentException - if n is not positive
Of course you should also handle the situation where your text boxes don't have a valid number in at all.
Finally, is there any reason why you're separating the declaration and assignment of rand
?
Random rand;
rand = new Random();
Why not just:
Random rand = new Random();
?
Upvotes: 4