android的计时器怎么用,如何使用android计时器

有一个问题试图了解如何在Android中使用计时器。我有一个活动,在我的onCreate方法中设置了timerTask。我按下一个按钮调用该任务,它应该做的就是附加一个textview。问题是我在timer.schedule(task, TIMER_DELAY, 3*TIMER_ONE_SECOND);得到一个NullPointerException

是否有任何明显的原因导致我失踪?

public class UseGps extends Activity

{

Button gps_button;

TextView gps_text;

LocationManager mlocManager;

TimerTask task;

Timer timer;

public final int TIMER_DELAY = 1000;

public final int TIMER_ONE_MINUTE = 60000;

public final int TIMER_ONE_SECOND = 1000;

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

gps_button = (Button) findViewById(R.id.Button);

gps_text = (TextView) findViewById(R.id.Text);

task = new TimerTask() {

@Override

public void run() {

gps_text.append("Time up ");

try {

this.wait(TIMER_DELAY);

}

catch (InterruptedException e){

}

}

};

/* Use the LocationManager class to obtain GPS locations */

double gps[] = getLastKnownGPS();

gps_text.setText("Last known Location = "+gps[0]+" "+gps[1]);

gps_button.setOnClickListener(new OnClickListener() {

public void onClick(View viewParam){

gps_text.append("nnSearching for current location. Please hold...");

gps_button.setEnabled(false);

mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

LocationListener mlocListener = new MyLocationListener();

mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

timer.schedule(task, TIMER_DELAY, 3*TIMER_ONE_SECOND);

}

});

}

}

提前致谢

凯文

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值