线程中的值未更新

Values not getting updated in Thread

我正在使用线程在 progressBar 中显示进度,但在线程中,计算仅完成一次,之后在所有其他迭代中,为进度条计算的值将变为 0。

  public class CustomThread implements Runnable {

  int progressTempValue;
  long factorForIncrementation;
  long totalDuration;

  public CustomThread(String name, long totalDuration) {

      this.totalDuration = totalDuration;
      factorForIncrementation = totalDuration / 100;
      Log.e("Thread1", "Total Duration :" + totalDuration);
      Log.e("Thread1", "Factor for mills is:" + totalDuration);
  }

  @Override
  public void run() {

      mCountDownTimer = new CountDownTimer(totalDuration, 1000) {
          @Override
          public void onTick(long millisUntilFinished) {

              new Handler(Looper.getMainLooper()).post(new Runnable() {
                  @SuppressLint("LogNotTimber")
                  @Override
                  public void run() {


                      estimatedMillis = estimatedMillis + incrFactMillis;
                      Log.e("Thread1", "estimated Millis:" + estimatedMillis);
                      final int pgVal = (int)(totalDuration / (estimatedMillis * 100));
                      Log.e("Thread1", "Progress Value :" + pgVal);
                      myProgressBar.setProgress(pgVal);
                      Log.e("Thread1", "-----");


                  } //run()

              });


          } // on tick end

          @Override
          public void onFinish() {

          }

      }; // cdt end

      mCountDownTimer.start();

  } // if end


}

不过这是我可以分享的日志输出..

E/Thread1: Total Duration is :166000
E/Thread1: Factor for mills is :1660
E/Thread1: estimated Millis:1660
E/Thread1: Progress Value :1
E/Thread1: -----
E/Thread1: estimated Millis:3320
E/Thread1: Progress Value :0
E/Thread1: -----
E/Thread1: estimated Millis:4980
E/Thread1: Progress Value :0
E/Thread1: -----
E/Thread1: estimated Millis:6640
E/Thread1: Progress Value :0
E/Thread1: -----
E/Thread1: estimated Millis:8300
E/Thread1: Progress Value :0
E/Thread1: -----
E/Thread1: estimated Millis:9960
E/Thread1: Progress Value :0
E/Thread1: -----
E/Thread1: estimated Millis:11620
E/Thread1: Progress Value :0

我的问题是,为什么要在 progressBar 中设置的计算值变为 0?

您执行的计算有误,导致除以值大于总持续时间。

Log.e("Thread1", "estimated Millis:" + estimatedMillis);
final int pgVal = (int)(totalDuration / (estimatedMillis * 100));

第一次追踪。

总时长 = 166000 estimatedMillis = 1660 * 100 = 166000 评估进度为 1.