每当我的EditText为空时我的应用程序崩溃,我按下按钮

时间:2018-03-22 17:01:41

标签: java android crash-reports

问题是,每次我点击我的应用程序中的按钮时,只要我的EditText或其中一个为空,该应用程序就会崩溃。

我的想法是,我会在名为caloriesIn的EditText中写出卡路里,并在int中显示caloriesOut这是一个文本字段。 同样的想法适用于“胖”。

总而言之,问题是如果我在卡路里中写东西,但不写任何东西,或者只是不在其中任何一个中写任何东西,那么应用程序就会崩溃。

我的代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                EditText caloriesIn = (EditText) findViewById(R.id.caloriesIn);
                EditText fatIn = (EditText) findViewById(R.id.fatIn);
                TextView caloriesOut = (TextView) findViewById(R.id.caloriesOut);
                TextView fatOut = (TextView) findViewById(R.id.fatOut);

                int calories = Integer.parseInt(caloriesIn.getText().toString());
                int fat = Integer.parseInt(fatIn.getText().toString());
                int caloriesResult = calories;
                int fatResult = fat;

                caloriesOut.setText(caloriesResult + "");
                fatOut.setText(fatResult + "");


            }
        });

    }
}

崩溃报告:

  

03-22 17:20:02.512 22193-22193 / ottolopez.healthynote I / Choreographer:跳过47帧!应用程序可能在其主线程上做了太多工作。   03-22 17:20:02.556 22193-22193 / ottolopez.healthynote V / View:dispatchProvideAutofillStructure():not layout out,忽略0孩子的1073741833   03-22 17:20:02.561 22193-22193 / ottolopez.healthynote I / AssistStructure:展平的最终辅助数据:2936字节,包含1个窗口,11个视图   03-22 17:20:05.047 22193-22193 / ottolopez.healthynote D / AndroidRuntime:关闭VM   03-22 17:20:05.049 22193-22193 / ottolopez.healthynote E / AndroidRuntime:FATAL EXCEPTION:main                                                                          过程:ottolopez.healthynote,PID:22193                                                                          java.lang.NumberFormatException:对于输入字符串:“”                                                                              在java.lang.Integer.parseInt(Integer.java:620)                                                                              在java.lang.Integer.parseInt(Integer.java:643)                                                                              at ottolopez.healthynote.MainActivity $ 1.onClick(MainActivity.java:28)                                                                              在android.view.View.performClick(View.java:6294)                                                                              在android.view.View $ PerformClick.run(View.java:24770)                                                                              在android.os.Handler.handleCallback(Handler.java:790)                                                                              在android.os.Handler.dispatchMessage(Handler.java:99)                                                                              在android.os.Looper.loop(Looper.java:164)                                                                              在android.app.ActivityThread.main(ActivityThread.java:6494)                                                                              at java.lang.reflect.Method.invoke(Native Method)                                                                              在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)                                                                              在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

3 个答案:

答案 0 :(得分:0)

在您的活动中定义此方法:

public boolean isParsable(String input){
    boolean parsable = true;
    try{
        Integer.parseInt(input);
    }catch(NumberFormatException e){
        parsable = false;
    }
    return parsable;
}

然后像这样更新你的代码:

     public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                EditText caloriesIn = (EditText) findViewById(R.id.caloriesIn);
                EditText fatIn = (EditText) findViewById(R.id.fatIn);
                TextView caloriesOut = (TextView) findViewById(R.id.caloriesOut);
                TextView fatOut = (TextView) findViewById(R.id.fatOut);
                String caloriesString = caloriesIn.getText().toString();
                int calories = 0;
                if (isParsable(caloriesString)){
                    calories = Integer.parseInt(caloriesString);
                    int caloriesResult = calories;
                    caloriesOut.setText(caloriesResult + "");
                }

                String fatString = caloriesIn.getText().toString();
                int fat = 0;
                if (isParsable(fatString)){
                    fat = Integer.parseInt(fatString);
                    int fatResult = fat;
                    fatOut.setText(fatResult + "");
                }

            }
        });

    }


    public static boolean isParsable(String input){
        boolean parsable = true;
        try{
            Integer.parseInt(input);
        }catch(NumberFormatException e){
            parsable = false;
        }
        return parsable;
    }

}

希望这有帮助

答案 1 :(得分:0)

事情是Integer.parseInt()没有处理空字符串,而不是数字。因此,您需要检查文本空白,然后使用Integer.parseInt()。您也可以在使用parseInt之前使用isDigitsOnly方法。

答案 2 :(得分:0)

 int calories = Integer.parseInt(caloriesIn.getText().toString());
 int fat = Integer.parseInt(fatIn.getText().toString());

主要问题在于这些行,它会给出错误,你在这里传递任何字符或留空,解决这些问题将这些行添加到你的代码中

String calIn =  caloriesIn.getText().toString(); //first take input in String for checking
    String fatInStr = fatIn.getText().toString(); //first take input in String for checking

    if (calIn.isEmpty()) {
         Toast.makeText(...);//inform user that calories input field is empty
        return;
    } else if (fatInStr.isEmpty()) {
         Toast.makeText(...);//inform user that fat input field is empty
        return;
    } else if (!calIn.matches("[0-9]+")) {
         Toast.makeText(...);//inform user that calories input field contains invalid data
        return;
    } else if (!fatInStr.matches("[0-9]+")) {
        // Toast.makeText(...);//inform user that fat input field contains invalid data
        return;
    }
    int calories = Integer.parseInt(calIn.trim()); // trim method removes any leading and trailing spaces
    int fat = Integer.parseInt(fatInStr.trim());

现在app在文本为空且不是数字时是安全的:)