我的活动有一个 ProgressBar。开始活动时,我会检查从另一个地方获取的值并更新到 ProgressBar。这是我的代码:
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar_detail);
final TextView progressText = (TextView) findViewById(R.id.progressText_detail);
final ImageView btnCancel = (ImageView) findViewById(R.id.imgCancel_detail);
progressBar.setVisibility(View.VISIBLE);
progressText.setVisibility(View.VISIBLE);
btnCancel.setVisibility(View.VISIBLE);
Thread t = new Thread() {
    public void run() {
        ((Activity) ctx).runOnUiThread(new Runnable() {
            public void run() {
                int oldProgress = 0;
                while (progressBar.getProgress() < 100) {
                    int value = Downloader.progress.get(gameId+ "");
                    if (value != oldProgress) {
                        oldProgress = value;
                        progressBar.setProgress(value);
                        progressText.setText(value + " %");
                    }
                }
            }
        });
    }
};
t.start();
我从中获得的 ProgressBar 的值是int value = Downloader.progress.get(gameId)正确的。但是当我运行此代码时,活动没有响应并且没有显示任何内容(但应用程序没有崩溃)。似乎更新 ProgressBar 的线程正在运行并阻止 UI 线程,因此未显示活动布局。
我的代码有什么问题?在这种情况下更新 ProgressBar 的正确方法是什么?