当我在格式化方法中输入 %0.02f 作为参数时,小费计算器应用程序上的 SeekBar 崩溃了。
tipAmountEditText.setText(String.format("%0.02f", tipAmount));
我通过删除整数部分解决了这个问题,因此变成了 %.02f
。关于这个问题,我唯一可以说的是它是使用 ChangeListener 弹出的。我不明白为什么这会成为一个问题,我希望有人能启发我。如果您需要查看大图,我的完整代码在我的 github 上:https://github.com/xamroc/TipCalc
private OnSeekBarChangeListener tipSeekBarListener = new OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
tipAmount = (tipSeekBar.getProgress()) * .01;
tipAmountEditText.setText(String.format("%.02f", tipAmount));
updateTipAndFinalBill();
}
};
最佳答案
在这里String.format("%0.02f", tipAmount)
你得到了
java.util.MissingFormatWidthException
//This Unchecked exception thrown when the format width is required.
原因:
%0.02f interprets as a floating point at least 0 wide.
Thats why it gives MissingFormatWidthException // as its assuming width to 0
所以改用 Following
String.format("%.02f", tipAmount)
关于java - 由于字符串格式导致 Seekbar 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18851267/