为什么Runnable中的方法只能运行一次?

时间:2015-04-19 15:08:41

标签: android runnable

正如您所见,方法 refreshProgressBars()只能工作一次,尽管Toast创建没有中断。 怎么可能?!

 bREnemyRegeneration = new Runnable() {
            @Override
            public void run() {
                refreshProgressBars(); //method works only 1 time
                Toast.makeText( //works always
                      BattleActivity.this, 
                      "THIS TOAST WILL BE SHOWN", 
                      Toast.LENGTH_SHORT).show();
                bHandlerTimers.postDelayed(bREnemyRegeneration, 3000); //start new iteration of this Runnable
            }
        };

以防方法 refreshProgressBars()

public void refreshProgressBars(){
    checkDeath();
    if (bHealthsBar.getLayoutParams().width < 30) bHealthsBar.getLayoutParams().width = 0;
    else bHealthsBar.getLayoutParams().width = (int) (PB_EN_HP_WIDTH * bEnemy.getHealth(false) / bEnemy.getHealth(true));
}

Runnable调用如下:

bHandlerTimers = new Handler();
bHandlerTimers.post(bREnemyRegeneration);

1 个答案:

答案 0 :(得分:0)

我编辑方法 refreshProgressBar(),如下所示: 代替 bHealthsBar.getLayoutParams().width = bWidth 我用了 bHealthsBar.setLayoutParams(new RelativeLayout.LayoutParams(bWidth, bHealthsBar.getLayoutParams().height));

而且......一切正常!问题已经结束。