搜索栏由于字符串格式而崩溃



当我在格式方法中输入%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

所以改用关注

String.format("%.02f", tipAmount)

最新更新