本文介绍了安卓抛出:IllegalArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不断收到一个IllegalArgumentException时,下面的方法是RAN:
公众诠释随机化(ArrayList中<整数GT;列表){
随机RDM =新的随机();
INT大小=则为list.size();
int的选择= rdm.nextInt(大小);
整卡= list.get(选择);
list.remove(选择);
返回卡;
}
下面是堆栈跟踪:
11-22 16:52:16.890:E / AndroidRuntime(24979):致命异常:主要
11-22 16:52:16.890:E / AndroidRuntime(24979):java.lang.RuntimeException的:无法启动的活动ComponentInfo {edu.marshall.meadows182.match / edu.marshall.meadows182.match.Match}:java.lang中.IllegalArgumentException
11-22 16:52:16.890:E / AndroidRuntime(24979):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1821)
11-22 16:52:16.890:E / AndroidRuntime(24979):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)
11-22 16:52:16.890:E / AndroidRuntime(24979):在android.app.ActivityThread.access $ 1500(ActivityThread.java:132)
11-22 16:52:16.890:E / AndroidRuntime(24979):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1038)
11-22 16:52:16.890:E / AndroidRuntime(24979):在android.os.Handler.dispatchMessage(Handler.java:99)
11-22 16:52:16.890:E / AndroidRuntime(24979):在android.os.Looper.loop(Looper.java:143)
11-22 16:52:16.890:E / AndroidRuntime(24979):在android.app.ActivityThread.main(ActivityThread.java:4263)
11-22 16:52:16.890:E / AndroidRuntime(24979):在java.lang.reflect.Method.invokeNative(本机方法)
11-22 16:52:16.890:E / AndroidRuntime(24979):在java.lang.reflect.Method.invoke(Method.java:507)
11-22 16:52:16.890:E / AndroidRuntime(24979):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
11-22 16:52:16.890:E / AndroidRuntime(24979):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-22 16:52:16.890:E / AndroidRuntime(24979):在dalvik.system.NativeStart.main(本机方法)
11-22 16:52:16.890:E / AndroidRuntime(24979):由:java.lang.IllegalArgumentException异常
11-22 16:52:16.890:E / AndroidRuntime(24979):在java.util.Random.nextInt(Random.java:186)
11-22 16:52:16.890:E / AndroidRuntime(24979):在edu.marshall.meadows182.match.Match.randomize(Match.java:200)
11-22 16:52:16.890:E / AndroidRuntime(24979):在edu.marshall.meadows182.match.Match.initArrays(Match.java:108)
11-22 16:52:16.890:E / AndroidRuntime(24979):在edu.marshall.meadows182.match.Match.onCreate(Match.java:218)
11-22 16:52:16.890:E / AndroidRuntime(24979):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
11-22 16:52:16.890:E / AndroidRuntime(24979):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)
11-22 16:52:16.890:E / AndroidRuntime(24979):11 ...更多
这是我穿过确实有他们的元素(或者通过,应该把它们放在那里方法传递)任何人都知道为什么是这样的ArrayList?
解决方案
从的
编辑:我尝试用这个code:
公共静态无效的主要(字符串的args []){
随机R =新的随机();
的System.out.println(r.nextInt(0)); //< - 错误
//它不能在0(含)返回一个值,... 0(不!)
}
的输出是一个IllegalArgumentExcepion:
在线程异常主要java.lang.IllegalArgumentException:如果n必须是正
在java.util.Random.nextInt(来源不明)
在Random0.main(Random0.java:7)
如果你的列表是空的,您的变量的大小可能是0,也就是不积极。你必须赶上这种特殊情况下,并对其进行管理。
I keep getting an IllegalArgumentException when the following method is ran:
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;
}
Here is the 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
The ArrayLists that I am passing through do have elements in them (or they pass through a method that should put them in there) Anyone know why this is?
解决方案
From javadoc:
EDIT: I try with this code:
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!!!)
}
The output is an 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)
If your list is empty, your variable size may be 0, that is not positive.You must catch this particular case, and manage it.
这篇关于安卓抛出:IllegalArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!