我做了一些关于计时器使用的研究,但我遇到的只是一个错误。
以下是相关代码:
package com.example.test;
import java.util.Timer;
import java.util.TimerTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
Toast wait = Toast.makeText(getApplicationContext(), "Wait", Toast.LENGTH_LONG);
wait.setGravity(Gravity.TOP, 0, 0);
wait.show();
}
};
Timer timer = new Timer();
timer.schedule(timerTask, 2000, 2000);
}
}
当我跑步时,我从上面的scipt中得到一个错误。错误在哪里?
答案 0 :(得分:1)
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
//Perform background work here
handler.post(new Runnable() {
public void run() {
//Perform GUI updation work here
//Toast work also
}
});
}
};
timer.schedule(doAsynchronousTask, 10, 5000);