为什么 Runnable 中的方法只能运行一次?
Why method in Runnable works only one time?
如您所见,方法 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);
我编辑方法 refreshProgressBar() 如下:
反而
bHealthsBar.getLayoutParams().width = bWidth
我用了
bHealthsBar.setLayoutParams(new RelativeLayout.LayoutParams(bWidth, bHealthsBar.getLayoutParams().height));
而且...一切正常!问题已关闭。
如您所见,方法 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);
我编辑方法 refreshProgressBar() 如下:
反而
bHealthsBar.getLayoutParams().width = bWidth
我用了
bHealthsBar.setLayoutParams(new RelativeLayout.LayoutParams(bWidth, bHealthsBar.getLayoutParams().height));
而且...一切正常!问题已关闭。