Android - random number generation crashing

So, I looked at other places to create a random number and put it in an Android app. I expect this to work, but unfortunately with this LogCat it crashes:

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 tried everything I could think of to make it work, but it does nothing. Here is 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)); } }; 
+4
source share
2 answers

Well, this will certainly happen if the max text field has a number less than or equal to your min text field. You must confirm this before calling Random.nextInt(int) . As stated in the docs:

Throws:
IllegalArgumentException - if n is not positive

Of course, you should also handle the situation where your text fields do not have a real number at all.

Finally, is there a reason you separate the declaration and the assignment of rand ?

 Random rand; rand = new Random(); 

Why not just:

 Random rand = new Random(); 

?

+4
source

java.lang.IllegalArgumentException indicates that you are passing the illegal nextInt() argument to you. I assume your argument is less than zero or something similar to this. Check these values ​​in the debugger to make sure your arguments are valid.

+1
source

Source: https://habr.com/ru/post/1443889/


All Articles