【问题标题】:Fatal Exception: java.lang.NumberFormatException on Toggle Buttons to assign double value [duplicate]致命异常:切换按钮上的 java.lang.NumberFormatException 分配双值 [重复]
【发布时间】:2020-07-30 22:41:40
【问题描述】:

我在某些设备中遇到此异常,主要是 API29。因此,我将双舍入值分配给切换按钮。当用户点击它们时,它会将该值添加到当前总计中,然后将该总计转换为字符串以便显示在 TextView 中。

我已尝试添加异常,但无法显示分配给切换按钮的双倍价格。

另外,我在这里查看了一些其他没有帮助的主题。

代码如下;有这方面的指导吗?

private void setListeners() {


        //boolean chemoIsChecked = false;

        chemoBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

                    if (isChecked) {
                        try{

                            Double chemoPrice = Double.valueOf(chemoButtonPrice);
                            d = chemoPrice + d;
                            String totalPrice = String.valueOf(String.format(Locale.GERMAN, "%.2f", d));
                            premiumTitle.setText("Premie per maand incl. BTW: € " + totalPrice);

                        }catch (NumberFormatException e){

                        }

                    } else {
                        try{

                        }catch (NumberFormatException e){
                            Double chemoPrice = Double.valueOf(chemoButtonPrice);
                            d = d - chemoPrice;
                            String totalPrice = String.valueOf(String.format(Locale.GERMAN,"%.2f", d));
                            premiumTitle.setText("Premie per maand incl. BTW: € " + totalPrice);
                        }

                    }

            }
        });

        cremBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    Double cremPrice = Double.valueOf(cremationButtonPrice);
                    d = cremPrice + d;
                    String totalPrice = String.valueOf(String.format(Locale.GERMAN,"%.2f", d));
                    premiumTitle.setText("Premie per maand incl. BTW: € " + totalPrice);
                } else {
                    Double cremPrice = Double.valueOf(cremationButtonPrice);
                    d = d - cremPrice;
                    String totalPrice = String.valueOf(String.format(Locale.GERMAN,"%.2f", d));
                    premiumTitle.setText("Premie per maand incl. BTW: € " + totalPrice);
                }
            }

        });

        travenBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (isChecked) {
                    Double travelPrice = Double.valueOf(travelButtonPrice);
                    d = travelPrice + d;
                    String totalPrice = String.valueOf(String.format(Locale.GERMAN,"%.2f", d));
                    premiumTitle.setText("Premie per maand incl. BTW: € " + totalPrice);
                } else {
                    Double travelPrice = Double.valueOf(travelButtonPrice);
                    d = d - travelPrice;
                    String totalPrice = String.valueOf(String.format(Locale.GERMAN,"%.2f", d));
                    premiumTitle.setText("Premie per maand incl. BTW: € " + totalPrice);
                }
            }

        });

        btnContinue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                createAdditionalPackages();


}
Fatal Exception: java.lang.NumberFormatException: For input string: "3,78"
       at sun.misc.FloatingDecimal.readJavaFormatString + 2043(FloatingDecimal.java:2043)
       at sun.misc.FloatingDecimal.parseDouble + 110(FloatingDecimal.java:110)
       at java.lang.Double.parseDouble + 538(Double.java:538)
       at java.lang.Double.valueOf + 502(Double.java:502)
       at nl.risk.www.smart2coverapp.activity.PetplanAdditionalPlansActivity$2.onCheckedChanged + 165(PetplanAdditionalPlansActivity.java:165)
       at android.widget.CompoundButton.setChecked + 182(CompoundButton.java:182)
       at android.widget.ToggleButton.setChecked + 71(ToggleButton.java:71)
       at android.widget.CompoundButton.toggle + 136(CompoundButton.java:136)
       at android.widget.CompoundButton.performClick + 141(CompoundButton.java:141)
       at android.view.View.performClickInternal + 7312(View.java:7312)
       at android.view.View.access$3200 + 846(View.java:846)
       at android.view.View$PerformClick.run + 27794(View.java:27794)
       at android.os.Handler.handleCallback + 873(Handler.java:873)
       at android.os.Handler.dispatchMessage + 99(Handler.java:99)
       at android.os.Looper.loop + 214(Looper.java:214)
       at android.app.ActivityThread.main + 7099(ActivityThread.java:7099)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 494(RuntimeInit.java:494)
       at com.android.internal.os.ZygoteInit.main + 965(ZygoteInit.java:965)

【问题讨论】:

  • "3,78" 尝试使用 3.78 的点而不是逗号

标签: java android exception togglebutton numberformatexception


【解决方案1】:

而不是做:

Double travelPrice = Double.valueOf(travelButtonPrice);

首先将逗号转换为带有replace的点:

Double travelPrice = Double.valueOf(travelButtonPrice.replace(",", "."));

【讨论】:

  • 这个应用程序在荷兰运行,我刚刚意识到我已将德国添加为区域设置。在荷兰,他们使用逗号而不是点。你如何解决这个问题?
  • 逗号是打印值。但是要将值存储为双精度值,您拥有的语言环境并不重要,您始终需要使用点作为分隔符来保存双精度值
  • 是,但仍在崩溃...
【解决方案2】:

请使用 String 类的 replace 方法删除逗号(,),如下所示

String totalPrice = String.valueOf(String.format(Locale.GERMAN,"%.2f", d)).replace(',', '').trim()

【讨论】:

    猜你喜欢
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 2018-05-27
    • 1970-01-01
    • 2017-07-14
    • 2016-06-10
    • 1970-01-01
    相关资源
    最近更新 更多