I'm having two issues displaying fps in my empty android game
===

To create the game loop i followed [this excellent tutorial.][1] But i thought the next tutorial on displaying the fps was a little iffy so i tried going solo. I'm reasonably confident in the trackFps method i made; it's called after calculating the time difference between frames, measures predicted fps everytime it runs, stores those predicted fps values in an ArrayList then once a second has passed adds up those predicted fps' and divides by the number of values added to get an average fps.

When i run through it with the debugger it functions fine, but when i run through it normally i get the following exception:

    FATAL EXCEPTION: Thread-11
    java.lang.IndexOutOfBoundsException: Invalid index 26, size is 6
    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
    at java.util.ArrayList.get(ArrayList.java:311)
    at biz.hireholly.engine.GameLoop.trackFps(GameLoop.java:120)
    at biz.hireholly.engine.GameLoop.run(GameLoop.java:73)

It's pretty explanatory and occurs on this line `fps += fpsStore.get(fpsTrackCount-1);` but i can't see how the `fpsTrackCount` Variable could be getting as high as 26, it should only be as high as the number of variable's stored in the arraylist `fpsStore`.

Would someone look through my GameLoop class, in particular the trackFps method at the bottom? i'll provide the whole thing. It's heavily commented but there's not much to it and should be very familiar for some of you.

**GameLoop (contains the trackFps method for calculating fps at the bottom):**

    package biz.hireholly.engine;
    
    import android.graphics.Canvas;
    import android.view.SurfaceHolder;
    import java.util.ArrayList;
    
    
    /**
     * The GameLoop is a thread that will ensure updating and drawing is done at set intervals.
     * The thread will sleep when it has updated/rendered quicker than needed to reach the desired fps.
     * The loop is designed to skip drawing if the update/draw cycle is taking to long, up to a MAX_FRAME_SKIPS.
     * The Canvas object is created and managed to some extent in the game loop,
     * this is so that we can prevent multiple objects trying to draw to it simultaneously.
     * Note that the gameloop has a reference to the gameview and vice versa.
     */
    
    public class GameLoop extends Thread {
    	
    	private static final String TAG = GameLoop.class.getSimpleName();
    	//desired frames per second
    	private final static int MAX_FPS = 30;
    	//maximum number of drawn frames to be skipped if drawing took too long last cycle
    	private final static int MAX_FRAME_SKIPS = 5;
    	//ideal time taken to update & draw
    	private final static int CYCLE_PERIOD = 1000 / MAX_FPS;
    
    	private SurfaceHolder surfaceHolder;
    	//the gameview actually handles inputs and draws to the surface
    	private GameView gameview;
    
    	private boolean running;
    	private long beginTime = 0; // time when cycle began
    	private long timeDifference = 0; // time it took for the cycle to execute
    	private int sleepTime = 0; // milliseconds to sleep (<0 if drawing behind schedule) 
    	private int framesSkipped = 0; // number of render frames skipped
    	
    	private double lastFps = 0; //The last FPS tracked, the number displayed onscreen
    	private int fpsTrackCount = 1; // number we'll divide the fpsSTore by to get average
    	private ArrayList<Double> fpsStore = new ArrayList<Double>(); //For the previous fps values
    	private long lastTimeFpsCalculated = System.currentTimeMillis(); //used in trackFps
    	
    	public GameLoop(SurfaceHolder holder, GameView gameview) {
    		super();
    		this.surfaceHolder = holder;
    		this.gameview = gameview;
    	}
    
    	public void setRunning(boolean running) {
    		this.running = running;		
    	}
    	@Override
    	public void run(){
    		
    		Canvas c;
    				
    		while (running) {
    			c = null;
    			//try locking canvas, so only we can edit pixels on surface
    			try{
    				c = this.surfaceHolder.lockCanvas();
    				//sync so nothing else can modify while were using it
    				synchronized (surfaceHolder){ 
    					
    					beginTime = System.currentTimeMillis();
    					framesSkipped = 0; //reset frame skips
    					
    					this.gameview.update();
    					this.gameview.draw(c);
    					
    					//calculate how long cycle took
    					timeDifference = System.currentTimeMillis() - beginTime;
    					//good time to trackFps?
    					trackFps();
    					
    					//calculate potential sleep time
    					sleepTime = (int)(CYCLE_PERIOD - timeDifference);
    					
    					//sleep for remaining cycle
    					if (sleepTime >0){
    						try{
    							Thread.sleep(sleepTime); //saves battery! :)
    						} catch (InterruptedException e){}
    					}
    					//if sleepTime negative then we're running behind
    					while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS){
    						//update without rendering to catch up
    						this.gameview.update();
    						//skip as many frame renders as needed to get back into
    						//positive sleepTime and continue as normal
    						sleepTime += CYCLE_PERIOD;
    						framesSkipped++;
    					}
    					
    				}
    
    			} finally{
    				//finally executes regardless of exception, 
    				//so surface is not left in an inconsistent state
    				if (c != null){
    					surfaceHolder.unlockCanvasAndPost(c);
    				}
    			}
    		}
    	
    	}
    
    	/* Calculates the average fps every second */
    	private void trackFps(){
    		long currentTime = System.currentTimeMillis();
    		
    		if(timeDifference != 0){
    			fpsStore.add((double)(1000 / timeDifference));
    		}
    		//If a second has past since last time average was calculated,
    		// it's time to calculate a new average fps to display
    		if ((currentTime - 1000) > lastTimeFpsCalculated){
    			int fps = 0;
    			int toDivideBy = fpsTrackCount;
    			while ((fpsStore !=  null) && (fpsTrackCount > 0 )){
    				fps += fpsStore.get(fpsTrackCount-1);
    				fpsTrackCount--;
    			}
    			lastFps = fps / toDivideBy;
    			lastTimeFpsCalculated = System.currentTimeMillis();
    			fpsTrackCount = 1;
    			fpsStore.clear();
    		}
    		else{	
    		fpsTrackCount++;
    		}
    	}
    	/* So That it can be drawn in the gameview */
    	public String getFps() {
    		return String.valueOf(lastFps);
    	}
    
    }

Also my second minor problem is that i notice that the fps being drawn using .drawText is overlapping, i.e. each call to draw a number is drawn overlaying the number that was before it! (Instead of replacing that number on screen)
The fps is drawn onscreen using a quick TextDrawable class i threw together and drawn in the draw method of my GameView (that extends Surface view).

**TextDrawable class**


    package biz.hireholly.gameplay;
    
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    
    /* test class, 
     * can be used to quicky draw text to screen or polished later and extended to create specific text objects.
     */
    
    public class TextDrawable {
    	
    	private String text;
    	private Paint paint;
    	int x = 25;
    	int y = 25;
    	
    	public TextDrawable(){
    		paint = new Paint();
    		paint.setColor(Color.WHITE); 
    		paint.setTextSize(16); 
    		paint.setAlpha(100);
    		paint.setAntiAlias(true);
    		paint.setFakeBoldText(false);	
    	}
    	
    	
    	public void setText(String s){
    		this.text = s;
    	}
    	
    	public void draw(Canvas c, int width, int height){
    		
    		c.drawText(text, x, y, paint); 
    	}
    
    }

I don't think there's anything wrong with my GameView but i'll upload it [here][2] for reference.


  [1]: http://obviam.net/index.php/the-android-game-loop/
  [2]: http://pastebin.com/YAxWiX2z

