i have a yahtzee (dice game) where a player rolls 5 dice. i use a countdown timer to do a small animation where all of the dice are displayed as a “1” then “2” then “3” then “4” then all of the dice should go to a random number. on the emulator and any phone i test it on, this seems to work correctly 100% of the time however… since i have released my game on the android market i have gotten hundreds of crash reports complaining of this error, and it seems have something to do with this animation. judging from the user messages, it looks like it goes to all “6’s” then gets a force close.
here is the code:
public void animateDize(){
new CountDownTimer(900,150){
Integer j = 0;
@Override
public void onTick(long millisUntilFinished) {
for(int i = 0; i < 5; i++){
if(DieSet[i]== 0){ // If dice are not clicked, animate them
imageButtons[i].setBackgroundResource(imageRes[j]);
}
}
j ++;
}
@Override
public void onFinish() {
randomDize(); // Set dice to random values
}
}.start();
}
Here is the error i am getting back from my crash report. is there perhaps i better way i could be animating this instead?
java.lang.ArrayIndexOutOfBoundsException at
com.surreall.yatzee.yatzee$7.onTick(yatzee.java:649) at
android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124) at
android.os.Handler.dispatchMessage(Handler.java:99) at
android.os.Looper.loop(Looper.java:123) at
android.app.ActivityThread.main(ActivityThread.java:4627) at
java.lang.reflect.Method.invokeNative(Native Method) at
java.lang.reflect.Method.invoke(Method.java:521) at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) at
dalvik.system.NativeStart.main(Native Method)
Based just on the code we see here, it looks like
jwill become larger and larger until it’s too large to be an index into the arrayimageRes, at which point you’ll get this exception. If we saw more code, I might have a different interpretation, but based just on this, that seems a likely explanation.