但我实现了在代码中没有错误,但是当我尝试在模拟器中执行时,它说不幸的是应用程序停止了。 问题必须出在这一部分,因为它只在klassenNr = 6
时停止int zufallsNr1;
int zufallsNr2 = 300;
int zufallsNr3 = 300;
int zufallsNr4 = 300;
int zufallsNr5 = 300;
if (klassenNr == 6) {
Random zufall = new Random();
int zufallsNr1 = zufall.nextInt(name[4].length);
while (zufallsNr1 == zufallsNr2 && zufallsNr2 == 300) {
zufallsNr2 = zufall.nextInt(name[4].length);
}
while (zufallsNr3 == zufallsNr2 && zufallsNr3 == zufallsNr1 && zufallsNr3 == 300) {
zufallsNr3 = zufall.nextInt(name[4].length);
}
while (zufallsNr4 == zufallsNr2 && zufallsNr4 == zufallsNr1 && zufallsNr4 == zufallsNr3
&& zufallsNr4 == 300) {
zufallsNr4 = zufall.nextInt(name[4].length);
}
while (zufallsNr5 == zufallsNr2 && zufallsNr5 == zufallsNr1 && zufallsNr5 == zufallsNr3
&& zufallsNr5 == zufallsNr4 && zufallsNr5 == 300) {
zufallsNr5 = zufall.nextInt(name[4].length);
}
valuelist = new String[] { name[4][zufallsNr1],
name[4][zufallsNr2], name[4][zufallsNr3],
name[4][zufallsNr4], name[4][zufallsNr5] };
} else {
if (klassenNr == 5) {
valuelist = topographienamen;
} else {
valuelist = name[klassenNr];
}
}
300远远超过名称[4] .length
logcat的:
05-30 01:46:09.725: I/ActivityManager(297): START u0 {cmp=com.jafapp.igeo/.FuenfteActivity (has extras)} from pid 12533
05-30 01:46:09.738: W/WindowManager(297): Failure taking screenshot for (328x583) to layer 21020
05-30 01:46:10.355: D/AndroidRuntime(12533): Shutting down VM
05-30 01:46:10.406: W/dalvikvm(12533): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-30 01:46:10.506: E/AndroidRuntime(12533): FATAL EXCEPTION: main
05-30 01:46:10.506: E/AndroidRuntime(12533): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jafapp.igeo/com.jafapp.igeo.FuenfteActivity}: java.lang.ArrayIndexOutOfBoundsException: length=254; index=300
05-30 01:46:10.506: E/AndroidRuntime(12533): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
05-30 01:46:10.506: E/AndroidRuntime(12533): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-30 01:46:10.506: E/AndroidRuntime(12533): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-30 01:46:10.506: E/AndroidRuntime(12533): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-30 01:46:10.506: E/AndroidRuntime(12533): at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 01:46:10.506: E/AndroidRuntime(12533): at android.os.Looper.loop(Looper.java:137)
05-30 01:46:10.506: E/AndroidRuntime(12533): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-30 01:46:10.506: E/AndroidRuntime(12533): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 01:46:10.506: E/AndroidRuntime(12533): at java.lang.reflect.Method.invoke(Method.java:511)
05-30 01:46:10.506: E/AndroidRuntime(12533): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-30 01:46:10.506: E/AndroidRuntime(12533): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-30 01:46:10.506: E/AndroidRuntime(12533): at dalvik.system.NativeStart.main(Native Method)
05-30 01:46:10.506: E/AndroidRuntime(12533): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=254; index=300
05-30 01:46:10.506: E/AndroidRuntime(12533): at com.jafapp.igeo.FuenfteActivity.onCreate(FuenfteActivity.java:449)
05-30 01:46:10.506: E/AndroidRuntime(12533): at android.app.Activity.performCreate(Activity.java:5104)
05-30 01:46:10.506: E/AndroidRuntime(12533): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-30 01:46:10.506: E/AndroidRuntime(12533): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-30 01:46:10.506: E/AndroidRuntime(12533): ... 11 more
05-30 01:46:10.585: W/ActivityManager(297): Force finishing activity com.jafapp.igeo/.FuenfteActivity
05-30 01:46:10.645: W/ActivityManager(297): Force finishing activity com.jafapp.igeo/.KlassenlisteActivity
05-30 01:46:11.189: W/ActivityManager(297): Activity pause timeout for ActivityRecord{40fbe7f0 u0 com.jafapp.igeo/.FuenfteActivity}
05-30 01:46:11.255: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property
05-30 01:46:11.775: E/SurfaceFlinger(37): ro.sf.lcd_density must be defined as a build property
答案 0 :(得分:0)
只要所有变量都是300,你的代码就会循环。但是如果其中一个变量不是300,那么在第一个循环之后,其余变量就永远不会循环。所以最后当你的254长String []数组被占用时,它们中的大多数将是300,这会导致异常。