为什么我的进度条不起作用?

时间:2014-01-12 02:17:13

标签: java android progress

package com.example.progressdialog;

import com.example.progressdialog.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

   private ProgressDialog progress;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      progress = new ProgressDialog(this);
   }


   public void open(View view){
      progress.setMessage("Start This Baby Up!");
      progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
      progress.setIndeterminate(false);
      progress.show();

   final int totalProgressTime = 100;

   final Thread t = new Thread(){

   @Override
   public void run(){

      int jumpTime = 0;
      while(jumpTime < totalProgressTime){
         try {
            sleep(500);
            jumpTime += 1;
            progress.setProgress(0);
         } catch (InterruptedException e) {


           e.printStackTrace();
         }

      }

   }
   };
   t.start();

   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {

      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }
}

我似乎无法让我的进度条真正向前发展。它只显示静态0/100 bar。我试图让它以平稳的方式前进,大约需要30-45秒才能完成。有人能告诉我我做错了什么吗?我是java的新手!谢谢!

1 个答案:

答案 0 :(得分:1)

引文:

progress.setProgress(0);

这应该是

progress.setProgress(jumpTime);

我想?

我想,这也会导致问题。您无法从后台线程访问UI组件。您需要使用Handler。请参阅此Example