致命异常:切换按钮上的java.lang.NumberFormatException以分配双精度值

问题描述 投票:0回答:2

我在某些设备(主要是API29)中遇到此异常。因此,请将“双舍入”值分配给“切换”按钮。当用户单击它们时,它将该值添加到当前总计中,然后将该总计转换为String以便在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)
java android exception togglebutton numberformatexception
2个回答
0
投票

代替做:

Double travelPrice = Double.valueOf(travelButtonPrice);

首先用replace将逗号转换为点:

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

0
投票

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

String totalPrice = String.valueOf(String.format(Locale.GERMAN,"%.2f", d)).replace(',', '').trim()
© www.soinside.com 2019 - 2024. All rights reserved.