我试图让一段代码定期运行。
这是我的代码:
int endTime = 52;
final double[] weights = new double[endTime];
for (int j = 0; j < endTime; j++) {
final int k = j;
newWeight = i.integrate(getCarbs(), getProt(), getFat(), newWeight,
height, age, PAL, gender, 7);
double percentChange = (newWeight - weight);
percentChange = percentChange * 100 / weight;
if(percentChange <-100){
percentChange = -100;
}
weights[j] = percentChange;
final DecimalFormat twoDForm = new DecimalFormat("0.00");
final Handler h = new Handler();
int time = 300*(j);
Runnable r = new Runnable() {
public void run() {
((TextView) findViewById(R.id.weightGainNumbers))
.setText("Week:\t" + (k + 1) + "\nWeight Change:\t"
+ twoDForm.format(weights[k]) + "%");
animate(weights[Math.abs(k - 1)], weights[k], false);
}
};
h.postDelayed(r, time);
}
动画仅需 100 毫秒。但是,当我运行它时,应用程序挂起,并且只开始做它应该在 j = 15 左右做的事情。有人知道这里出了什么问题吗?