gpt4 book ai didi

安卓 IllegalArgumentException

转载 作者:行者123 更新时间:2023-11-29 14:55:20 27 4
gpt4 key购买 nike

运行以下方法时,我不断收到 IllegalArgumentException:

public int randomize(ArrayList<Integer> list){
Random rdm = new Random();
int size = list.size();
int chosen = rdm.nextInt(size);
Integer card = list.get(chosen);
list.remove(chosen);
return card;
}

这是 StackTrace:

11-22 16:52:16.890: E/AndroidRuntime(24979): FATAL EXCEPTION: main
11-22 16:52:16.890: E/AndroidRuntime(24979): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.marshall.meadows182.match/edu.marshall.meadows182.match.Match}: java.lang.IllegalArgumentException
11-22 16:52:16.890: E/AndroidRuntime(24979): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1821)
11-22 16:52:16.890: E/AndroidRuntime(24979): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)
11-22 16:52:16.890: E/AndroidRuntime(24979): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
11-22 16:52:16.890: E/AndroidRuntime(24979): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
11-22 16:52:16.890: E/AndroidRuntime(24979): at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 16:52:16.890: E/AndroidRuntime(24979): at android.os.Looper.loop(Looper.java:143)
11-22 16:52:16.890: E/AndroidRuntime(24979): at android.app.ActivityThread.main(ActivityThread.java:4263)
11-22 16:52:16.890: E/AndroidRuntime(24979): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 16:52:16.890: E/AndroidRuntime(24979): at java.lang.reflect.Method.invoke(Method.java:507)
11-22 16:52:16.890: E/AndroidRuntime(24979): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-22 16:52:16.890: E/AndroidRuntime(24979): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-22 16:52:16.890: E/AndroidRuntime(24979): at dalvik.system.NativeStart.main(Native Method)
11-22 16:52:16.890: E/AndroidRuntime(24979): Caused by: java.lang.IllegalArgumentException
11-22 16:52:16.890: E/AndroidRuntime(24979): at java.util.Random.nextInt(Random.java:186)
11-22 16:52:16.890: E/AndroidRuntime(24979): at edu.marshall.meadows182.match.Match.randomize(Match.java:200)
11-22 16:52:16.890: E/AndroidRuntime(24979): at edu.marshall.meadows182.match.Match.initArrays(Match.java:108)
11-22 16:52:16.890: E/AndroidRuntime(24979): at edu.marshall.meadows182.match.Match.onCreate(Match.java:218)
11-22 16:52:16.890: E/AndroidRuntime(24979): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
11-22 16:52:16.890: E/AndroidRuntime(24979): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)
11-22 16:52:16.890: E/AndroidRuntime(24979): ... 11 more

我正在传递的 ArrayLists 中确实有元素(或者它们通过应该将它们放在那里的方法传递)有人知道这是为什么吗?

最佳答案

来自 javadoc:

public int nextInt(int n)

Throws:

   IllegalArgumentException - n is not positive.

编辑:我尝试使用这段代码:

public static void main(String args[]) {
Random r = new Random();

System.out.println(r.nextInt(0)); // <-- error
// It cannot return a value between 0 (inclusive) and... 0 (exclusive!!!)
}

输出是一个 IllegalArgumentExcepion:

Exception in thread "main" java.lang.IllegalArgumentException: n must be positive
at java.util.Random.nextInt(Unknown Source)
at Random0.main(Random0.java:7)

如果您的列表为空,您的变量大小可能为 0,这不是正值。您必须捕获这种特殊情况并加以管理。

关于安卓 IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8234392/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com