java - 由于字符串格式导致 Seekbar 崩溃

标签 java android seekbar changelistener

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

Docs

原因:

%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/

相关文章:

java - 将对象附加到现有文件

java - Java EE规范的实现在Scala中能否更高效?

android - Marshmallow 上的低对比度 MediaStyle 通知

android - 如何解决 Unable to find explicit activity in firebase AuthUi?

android - 在android中实现简单的SeekBar

java - Android/Java 中的网络 - 如何在从服务器回调后回调到 API 类?

java - DocuSign Java API EnvelopesApi.getDocument 返回 NULL 异常

android - 在运行时膨胀类 CardView 时出错

android - SeekBarPreference 似乎没有保留其值(value)

Android 搜索栏 : Changing the value