为什么我得到"无法解决方法scheduleAtFixedRate"?

时间:2015-08-22 14:51:15

标签: android class timertask

我不知道为什么我收到这个错误。通过我在班级TimerTask中看到的所有示例,这不应该导致问题。

public class CountUp extends AppCompatActivity {

EditText upText = (EditText) findViewById(R.id.Up);
int counter = 0;
float StartTime = 0;
float OffsetTime = 1000;    //Offset time is the time between event. 1000 is going to be our milliseconds (1 second)

TimerTask tt = new TimerTask() {
    @Override
    public void run() {
        upText.setText(counter);
        counter++;
    }
};

public void CountUp(View view){
    try {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(tt,StartTime,OffsetTime); <--- //This is were I am receiving an error
    }catch (Exception e){

    }
}

1 个答案:

答案 0 :(得分:2)

用以下代码替换整个代码:

public class CountUp extends AppCompatActivity {

    EditText upText;
    TimerTask tt;
    int counter = 0;

    //startTime and offsetTime must be long and not float.
    long startTime = 0;
    long offsetTime = 1000;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Change this to your current layout.
        setContentView(R.layout.main_activity);

        upText = (EditText) findViewById(R.id.Up);

        tt = new TimerTask() {
            @Override
            public void run() {
                upText.setText(String.valueOf(counter));
                counter++;
            }
        };
    }

    public void countUp(View view) {
        try {
            Timer timer = new Timer();
            timer.scheduleAtFixedRate(tt, startTime, offsetTime);
        } catch (Exception e) {

        }
    }
}