【发布时间】:2011-12-06 08:44:52
【问题描述】:
更新: 这篇文章是我在用android 2.2学习android的时候写的,一定要检查它是否与你正在使用的api级别兼容。
为如何使用计时器加载进度条做了很多工作,我尝试使用已发布的方法,但总是会出现指针异常,或者测试应用程序会在启动时崩溃。
我的问题是,大声笑有没有人跑过任何关于如何做到这一点的教程或示例?我在想我需要的只是一个while循环,但还没有看到在计时器上完成。我是一个完全的菜鸟,但正在缓慢但肯定地学习
我的计时器
final Timer t = new Timer();
t.schedule(new TimerTask() {
public void run() {
d.dismiss(); //MY DIALOG WINDOW
t.cancel();
}
}, 7000);
这是我试图开始工作的一个,但我认为我对它造成的伤害比它好
isRunning = false;
// handler for the background updating
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
total = total - 1;
String perc = String.valueOf(total).toString();
a.setText(perc + " SECONDS until close");
bar.incrementProgressBy(25);
}
};
super.onStart();
// reset the bar to the default value of 0
bar.setProgress(0);
// create a thread for updating the progress bar
Thread background = new Thread(new Runnable() {
public void run() {
try {
for (int i = 4; i < 20 && isRunning; i++) {
// wait 1000ms between each update
Thread.sleep(1000);
handler.sendMessage(handler.obtainMessage());
}
} catch (Throwable t) {
}
}
});
isRunning = true;
// start the background thread
background.start();
希望有人能够为我阐明这一点,一如既往地为简短的通用问题感到抱歉,只是想了解如何使用计时器加载进度条
再次感谢所有帮助
【问题讨论】: