我根据此网站http://www.mkyong.com/android/android-progress-bar-example/
进行对话当进度条变为100%时,对话框已关闭,但仍会连续显示吐司。我注意到,在bar变为100%之后,progressHandler仍然会循环运行。
我该如何解决这个问题? 我想要的东西:当进度条变为100%时,对话框关闭,Toast显示并关闭。
Thread background = new Thread(new Runnable() {
@SuppressWarnings("null")
public void run() {
try {
while (dialog.getProgress() <= dialog.getMax()) {
// wait 500ms between each update
Thread.sleep(100);
// active the update handler
progressHandler.sendMessage(progressHandler.obtainMessage());
}
} catch (java.lang.InterruptedException e) {
// if something fails do something smart
Toast.makeText(getApplicationContext(), "Error Occurs",Toast.LENGTH_LONG).show();
}
}
});
// start the background thread
background.start();
}
// handler for the background updating
Handler progressHandler = new Handler() {
public void handleMessage(Message msg) {
dialog.incrementProgressBy(increment);
if(dialog.getProgress()==100){
editor.putInt("lastestSummaryNoSent",summary.getCurrentSummaryNo());
editor.commit();
dialog.dismiss();
Toast.makeText(getApplicationContext(), "Update Finished", Toast.LENGTH_LONG).show();
}
}
};
答案 0 :(得分:0)
此错误
无法在未调用Looper.prepare()
的线程内创建处理程序
是因为你在背景线程中提供的Toast。删除Catch短语中的Toast,你会很高兴。