Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我的应用程序突然停止了_Android_Arrays_Exception_Indexing - Fatal编程技术网

Android 我的应用程序突然停止了

Android 我的应用程序突然停止了,android,arrays,exception,indexing,Android,Arrays,Exception,Indexing,我最近把我的应用程序上传到谷歌游戏商店 但时不时会发生这种情况。我在这里得到了错误报告: java.lang.ArrayIndexOutOfBoundsException: length=3; index=3 at de.krissini.server.Animation.getImage(Animation.java:36) 这是参考代码: public class Animation { private Bitmap[] frames; private int c

我最近把我的应用程序上传到谷歌游戏商店 但时不时会发生这种情况。我在这里得到了错误报告:

java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
    at de.krissini.server.Animation.getImage(Animation.java:36)
这是参考代码:

public class Animation {

    private Bitmap[] frames;
    private int currentFrame;
    private long startTime, delay;
    private boolean playedOnce;

    public void setFrames(Bitmap[] frames){
        this.frames = frames;
        currentFrame = 0;
        startTime = System.nanoTime();
    }

    public void setDelay(long d){
        delay = d;
    }
    public void setFrame(int i){
        currentFrame = i;
    }
    public void update(){
        long elapsed = (System.nanoTime()-startTime)/1000000;
        if(elapsed>delay){
            currentFrame++;
            startTime = System.nanoTime();
        }
        if(currentFrame == frames.length){
            currentFrame = 0;
            playedOnce = true;
        }
    }
    public Bitmap getImage(){
        return frames[currentFrame];     // line 36
    }
    public int getFrame(){
        return currentFrame;
    }
    public boolean playedOnce(){
        return playedOnce;
    }

}
我理解这个错误,但我怎样才能防止它呢?
我很欣赏每一个答案。这个错误是由于错误地认为数组的长度与最大索引相同造成的。当长度从1开始,索引从0开始时,它们总是不同于1

更改此项:

if(currentFrame == frames.length ){
    // your reset code
}
为此:

if(currentFrame == frames.length - 1){
    // your reset code
}