首页 > 解决方案 > Android Integer.parseInt 负数

问题描述

如果我的问题很简单,我是开发新手,很抱歉。我正在为 D&D 开发一个应用程序。当用户在第一个edittext中插入一个数字时,我使用edittext的onTextChanged,所以我将结果设置为第二个edittex。

我的问题仅在检查复选框时才发生。如果未选中该复选框,则它可以正常工作,但如果选中该复选框,则应用程序将进行总和(熟练度奖励 + mod)。它有效,但仅适用于正数。当应用程序设置为 fe 时-5,应用程序崩溃。数据全部保存在 sharedpreferences 中。

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

                float result_num;
                int num1, num2;
                int f= 0;

                DecimalFormat df = new DecimalFormat("0.##########");

                num1 = Integer.parseInt(proficiencybonus.getText().toString());

                if (strength_mod.getText().toString().length() > 0) {
                    num2 = Integer.parseInt(strength_mod.getText().toString());

                } else {
                    num2=f;

                }

                result_num = num1 + num2;
                strength_save.setText(" = " + df.format(result_num));


                editor.putBoolean("checkBox_strength", true);
                editor.apply();
            }else{
                editor.putBoolean("checkBox_strength", false);
                editor.apply();
            }
        }
    });

尝试了其他方法,但我无法找到解决方案

float result_num;
                int num1, num2;
                int f= 0;

                DecimalFormat df = new DecimalFormat("0.##########");

                num1 = Integer.parseInt(proficiencybonus.getText().toString());

                if (strength_mod.getText().toString().length() > 0) {
                    num2 = Integer.parseInt(strength_mod.getText().toString());

                } else {
                    num2=f;

                }

                result_num = num1 + Math.abs(num2);
                strength_save.setText(" = " + df.format(result_num));

这是我的崩溃错误,它崩溃了“num2 = Integer.parseInt(strength_mod.getText().toString());”

2020-05-03 17:09:24.440 10318-10318/jekanapplication.charactersheet5e E/AndroidRuntime: FATAL EXCEPTION: main
Process: jekanapplication.charactersheet5e, PID: 10318
java.lang.NumberFormatException: For input string: "−1"
    at java.lang.Integer.parseInt(Integer.java:608)
    at java.lang.Integer.parseInt(Integer.java:643)
    at jekanapplication.charactersheet5e.MainActivity$51.onCheckedChanged(MainActivity.java:1394)
    at android.widget.CompoundButton.setChecked(CompoundButton.java:172)
    at android.widget.CompoundButton.toggle(CompoundButton.java:128)
    at android.widget.CompoundButton.performClick(CompoundButton.java:133)
    at android.view.View$PerformClick.run(View.java:24931)
    at android.os.Handler.handleCallback(Handler.java:808)
    at android.os.Handler.dispatchMessage(Handler.java:101)
    at android.os.Looper.loop(Looper.java:166)
    at android.app.ActivityThread.main(ActivityThread.java:7529)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

在此处输入图像描述

标签: androidtextviewandroid-edittext

解决方案


问题是如何在第二个edittext中设置文本,使用第一个edittext的onTextchange,我设置文本edittext.settext“-2”我在edittext.settext“R.String.two_”中更改了所以当我得到当我得到字符串时,我没有问题的文本。


推荐阅读