为什么我得到 "Cannot resolve method scheduleAtFixedRate"?
Why do I get "Cannot resolve method scheduleAtFixedRate"?
我不知道为什么会收到此错误。对于我在 class 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){
}
}
用这个替换你的整个代码:
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) {
}
}
}
我不知道为什么会收到此错误。对于我在 class 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){
}
}
用这个替换你的整个代码:
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) {
}
}
}