【发布时间】: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