我试图使用CountDownTimer每秒将值放入一个数组,但它不起作用

时间:2014-08-15 21:44:46

标签: java android timer

public class Calculator {

float[] countPerSec = new float[600];
float[] finishedArray = new float[600];
float[] omegaArray = new float[600];
float[] sortedArray = new float[600];
int radius = 55;
float windSpeedArray[] = new float[600];
float windspeedAvg = 0;
float[] windspeedMinMax = new float[2];
int timerArrayCounter = 0;



int sensorCountValue = 20;  //**count per second returned by the sensor**




//this function will update x values from 0 to 599 per second
public float[] fetchPerSecValues(){


    new CountDownTimer(60000, 1000) {

         public void onTick(long millisUntilFinished) {


            // while(timerArrayCounter == 59){
             Log.d("Timer","Running Clock");
                 countPerSec[timerArrayCounter++] = sensorCountValue;

                 String abc = Float.toString(countPerSec[timerArrayCounter]);
                 Log.d("Timer", abc);
            // }


         }

         public void onFinish() {

             for(int c = 0; c < 60; c++){
            finishedArray[c] = countPerSec[c];
            omegaArray[c] = (float) (finishedArray[c]*2*3.14);
            windSpeedArray[c] = radius*omegaArray[c]; 

              }


             Log.d("Here","Finished Clock");
                timerArrayCounter = 0;
                fetchPerSecValues();




         }
      }.start();
    return windSpeedArray;


}

计时器功能运行但是sensorCountValue的值20不会每秒放入数组。这里我的错误是什么? 另一件事是当我运行没有计时器的代码时我得到ArrayOutOfBounds异常。又一次,这是什么错误? 帮助赞赏!

1 个答案:

答案 0 :(得分:1)

该值被放入数组中,问题是您在插入时正在推进索引,因此您的日志语句实际上是获取数组中的下一个条目:

             countPerSec[timerArrayCounter++] = sensorCountValue;

             String abc = Float.toString(countPerSec[timerArrayCounter]);
             Log.d("Timer", abc);

仔细查看logcat输出中的堆栈跟踪,它会指向数组超出范围的异常。