进度条完成后不会进入

时间:2015-12-24 04:15:18

标签: android progress-bar visibility

我正在使用水平进度条。它工作正常,但在完成时间后不会消失。

progressBar = (ProgressBar) findViewById(R.id.progressBar1);
progressBar.setRotation(180);
new Thread(new Runnable() {
    public void run() {
        while (progressStatus < 21) {
            progressStatus += 1;
            handler.post(new Runnable() {
                public void run() {
                    progressBar.setProgress(progressStatus);
                    }
            });
            try {
                Thread.sleep(110);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        progressBar.setVisibility(View.GONE);
        img.setVisibility(View.GONE);
    }
}).start();

}

while语句结束时的setVisibility()函数崩溃了我的应用程序。

logcat的:

12-24 09:55:54.502    7160-7245/com.food.foodjugaad E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-25568
Process: com.food.foodjugaad, PID: 7160
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
        at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6462)
        at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:897)
        at android.view.View.requestLayout(View.java:17494)
        at android.view.View.requestLayout(View.java:17494)
        at android.view.View.requestLayout(View.java:17494)
        at android.view.View.requestLayout(View.java:17494)
        at android.view.View.requestLayout(View.java:17494)
        at android.view.View.requestLayout(View.java:17494)
        at android.view.View.requestLayout(View.java:17494)
        at android.view.View.setFlags(View.java:9726)
        at android.view.View.setVisibility(View.java:6698)
        at android.widget.ProgressBar.setVisibility(ProgressBar.java:1546)
        at com.food.foodjugaad.Swipe$2.run(Swipe.java:95)
        at java.lang.Thread.run(Thread.java:818)

2 个答案:

答案 0 :(得分:1)

android只有1个线程来更新UI,它是UI线程,任何其他线程都无法更新UI,如果你这样做会抛出异常。我编辑了你的代码。你能试试吗?

 progressBar = (ProgressBar) findViewById(R.id.progressBar1);
progressBar.setRotation(180);
new Thread(new Runnable() {
    public void run() {
        while (progressStatus < 21) {
            progressStatus += 1;
            handler.post(new Runnable() {
                public void run() {
                    progressBar.setProgress(progressStatus);
                    }
            });
            try {
                Thread.sleep(110);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        runOnUiThread(new Runnable() {
            public void run(){                     
                progressBar.setVisibility(View.GONE);
                img.setVisibility(View.GONE);
            }
        });
    }
}).start();

答案 1 :(得分:0)

您只能从主线程修改小部件。做另一个runnable来改变可见性。