Android 进度条与线程

发布于 2025-01-04 15:13:55 字数 1816 浏览 3 评论 0原文

我正在 android 中开发 ProgressBar 类,但我无法让它在 5 秒内进度并加载应用程序。一切正常,但进度条没有进展。这是代码。

public class StartPoint extends Activity{

ProgressBar progressBar;
private int progressBarStatus = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    progressBar = (ProgressBar)findViewById(R.id.progressBar1);


    Thread timer = new Thread(){
        public void run(){
            try{
                sleep(5000);
                while(progressBarStatus < 5000){
                    progressBar.setProgress(progressBarStatus);
                    progressBarStatus += 1000;

                }
            }catch(InterruptedException e){
                e.printStackTrace();
            }finally{
                Intent openMainList = new Intent(StartPoint.this, in.isuru.caf.MainList.class);
                startActivity(openMainList);
            }
        }
    };
    timer.start();
}

protected void onPause(){
    super.onPause();
    finish();
}

}

这是布局文件splash.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/mary_mother_of_god" />

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1.67" />

</LinearLayout>

I am working on ProgressBar class in android, but I can't make it progress through 5 seconds and load the application. Everything works but the progress bar not progressing. Here is the code.

public class StartPoint extends Activity{

ProgressBar progressBar;
private int progressBarStatus = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    progressBar = (ProgressBar)findViewById(R.id.progressBar1);


    Thread timer = new Thread(){
        public void run(){
            try{
                sleep(5000);
                while(progressBarStatus < 5000){
                    progressBar.setProgress(progressBarStatus);
                    progressBarStatus += 1000;

                }
            }catch(InterruptedException e){
                e.printStackTrace();
            }finally{
                Intent openMainList = new Intent(StartPoint.this, in.isuru.caf.MainList.class);
                startActivity(openMainList);
            }
        }
    };
    timer.start();
}

protected void onPause(){
    super.onPause();
    finish();
}

}

And here is the layout file splash.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/mary_mother_of_god" />

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1.67" />

</LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘东忘西忘不掉你 2025-01-11 15:13:55

您无法从不同的线程更新 UI Widget。你需要做类似的事情:

Thread timer = new Thread(){
    public void run(){
        try{
            sleep(5000);
            while(progressBarStatus < 5000){
                StartPoint.this.runOnUIThread(new Runnable(){
                    public void run()
                    {
                        progressBar.setProgress(progressBarStatus);
                        progressBarStatus += 1000;
                    }
                });

            }
        }catch(InterruptedException e){
            e.printStackTrace();
        }finally{
            Intent openMainList = new Intent(StartPoint.this, in.isuru.caf.MainList.class);
            startActivity(openMainList);
        }
    }
};
timer.start();

You can't update a UI Widget from a different thread. You need to do something like:

Thread timer = new Thread(){
    public void run(){
        try{
            sleep(5000);
            while(progressBarStatus < 5000){
                StartPoint.this.runOnUIThread(new Runnable(){
                    public void run()
                    {
                        progressBar.setProgress(progressBarStatus);
                        progressBarStatus += 1000;
                    }
                });

            }
        }catch(InterruptedException e){
            e.printStackTrace();
        }finally{
            Intent openMainList = new Intent(StartPoint.this, in.isuru.caf.MainList.class);
            startActivity(openMainList);
        }
    }
};
timer.start();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文