【问题标题】:My app crashes whenever my EditText is empty and I press a button每当我的 EditText 为空并且按下按钮时,我的应用程序就会崩溃
【发布时间】:2018-03-22 17:01:40
【问题描述】:

问题是每次我点击应用中的按钮时,每当我的EditText 之一或两者都为空时,应用就会崩溃。

我的想法是,我将在 EditText 中写入卡路里,称为caloriesIn,它会在 caloriesOut 中输出一个 int,这是一个文本字段。 “胖”也是如此。

问题只是总结一下,如果我在卡路里中写了一些东西,但没有在脂肪中写任何东西,或者只是在其中任何一个中都不写任何东西,应用程序就会崩溃。

我的代码:

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(): 未布局,忽略 1073741833 的 0 个孩子 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: 关闭虚拟机 03-22 17:20:05.049 22193-22193/ottolopez.healthynote E/AndroidRuntime: 致命异常: main 进程:ottolopez.healthynote,PID:22193 java.lang.NumberFormatException:对于输入字符串:“” 在 java.lang.Integer.parseInt(Integer.java:620) 在 java.lang.Integer.parseInt(Integer.java:643) 在 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) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

【问题讨论】:

  • 添加有问题的崩溃报告。
  • 我在哪里可以找到那个?
  • @Otto 应用程序崩溃后它会立即出现在 android studio 的 logcat 窗口中
  • 我该如何编辑我的帖子?
  • 您的问题下方有编辑按钮 ....

标签: java android crash-reports


【解决方案1】:

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

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;
    }

}

希望对你有帮助

【讨论】:

  • 很好,因为每次我只将一个变量放入一个 EditText 中时,它也会改变另一个
  • 对不起,我犯了一个小错误,再次查看更新的代码
  • 现在好了?
  • 对不起。它几乎可以工作,但是每当我在其中一个 EitText 中输入一个数字时,它都会改变两个 TextField,但我以后可能会解决这个问题。谢谢你:)
  • 似乎没有任何改变。顺便说一句,我非常感谢您的帮助
【解决方案2】:
 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());

现在,当文本为空且不是数字时,应用程序是安全的 :)

【讨论】:

    【解决方案3】:

    问题是Integer.parseInt() 不处理不是数字的空字符串。因此,您需要检查文本是否为空,然后使用Integer.parseInt()。您也可以在使用 parseInt 之前使用 isDigitsOnly 方法。

    【讨论】:

    • 好的,那么我如何检查文本是否为空?
    • 您可以使用方便的方法 TextUtils.isEmpty(caloriesIn.getText().toString())。如果它返回 false,则可以使用 parseInt。还使用数字作为编辑文本的 inputType,以便用户只能在您的编辑文本中输入数字。
    • TextUtils.isEmpty(caloriesIn.getText().toString())
    • 但是我如何把它变成一个 if 语句呢?
    猜你喜欢
    • 2016-10-18
    • 2017-10-23
    • 1970-01-01
    • 2022-12-29
    • 2011-06-05
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多