0

我根据这个网站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();
    }


}
};
4

1 回答 1

0

这个错误

无法在未调用 Looper.prepare() 的线程内创建处理程序

是因为您在后台Thread 中提供了 Toast。删除 Catch 短语中的 Toast 即可。

于 2012-09-07T11:04:19.677 回答