为什么整数从一个 Android 应用 Activity 发送到另一个导致我的应用崩溃?

我有一个 [DONE] 按钮,用于检查 2 个条目的结果。其中一个是[EditText--inputType:number],另一个是[TextView],当按下某个按钮时会增加。


我想要做的是检查 EditText 是否有整数或为空,并检查 TextView 的内容。如果它们都大于零。我将它们加起来并将总数发送到我的主要活动。这是我到目前为止的代码。


    public void returnbtn(View view) {


    // Initialize insert textView

    EditText insertcountBtn = findViewById(R.id.insertPushup);


    // Initialize counter textView

    TextView givencountBtn = findViewById(R.id.showCount);


    // get added int stuff from the insert textField

    int insertcountInt = 

    Integer.parseInt(insertcountBtn.getText().toString());


    // get string stuff from counter textView

    String givencountString = givencountBtn.getText().toString();


    // convert counter textView to int.

    Integer givencountInt = Integer.parseInt(givencountString);


    if (givencountInt <= 0 && insertcountInt <= 0){

        Total = 0;

    }  else if (givencountInt > 0 && insertcountInt <= 0) {


        Total = givencountInt;

    } else if (givencountInt <= 0 && insertcountInt > 0) {

        Total = insertcountInt;

    } else if (givencountInt > 0 && insertcountInt > 0){

        // Add Counter textView and Insert textView to an Int Total

        Total = givencountInt + insertcountInt;

    }


    // Create an Intent to return to the mainActivity.

    Intent beginPushup = new Intent(this, MainActivity.class);


    // Pass the current number to the push-up Counter activity.

    beginPushup.putExtra(TOTAL_DONE, Total);


    // Start mainActivity.

    startActivity(beginPushup);


}

我遇到的问题是 textView 或 EditText 我不确定。我所知道的是,如果我同时填写它们并单击完成,它会将它们相加并按预期将总数转移到 mainActivity。如果我将值添加到 EditText 并将 TextView 保留为 0,它也会执行它的意图。但是,如果我增加 TextView 并将 EditText 留空,它不会传输我的 TextView 整数并使应用程序崩溃。


我是否可以检测到 editText 错误,因为我认为这就是原因。如果是这样,正确的方法是什么?


万千封印
浏览 124回答 4
4回答

慕哥6287543

正如另一个答案所解释的那样,您的代码的问题在于,当 EditText 为空时,解析“null”会导致异常。因此,您只需要确保如果内容为空,则只需使用 0(零)值。你可以试试这个:public void returnbtn(View view) {// Initialize insert textViewEditText insertcountBtn = findViewById(R.id.insertPushup);// Initialize counter textViewTextView givencountBtn = findViewById(R.id.showCount);int insertcountInt = 0;int givencountInt = 0;// get added int stuff from the insert textFieldif (!TextUtils.isEmpty(insertcountBtn.getText()) && TextUtils.isDigitsOnly(insertcountBtn.getText())) {&nbsp; &nbsp; insertcountInt = Integer.parseInt(insertcountBtn.getText().toString());}// get string stuff from counter textViewString givencountString = givencountBtn.getText().toString();if (!TextUtils.isEmpty(givencountString) && TextUtils.isDigitsOnly(givencountString)) {&nbsp; &nbsp; givencountInt = Integer.parseInt(givencountString);}if (givencountInt <= 0 && insertcountInt <= 0){&nbsp; &nbsp; Total = 0;}&nbsp; else if (givencountInt > 0 && insertcountInt <= 0) {&nbsp; &nbsp; Total = givencountInt;} else if (givencountInt <= 0 && insertcountInt > 0) {&nbsp; &nbsp; Total = insertcountInt;} else if (givencountInt > 0 && insertcountInt > 0){&nbsp; &nbsp; // Add Counter textView and Insert textView to an Int Total&nbsp; &nbsp; Total = givencountInt + insertcountInt;}// Create an Intent to return to the mainActivity.Intent beginPushup = new Intent(this, MainActivity.class);// Pass the current number to the push-up Counter activity.beginPushup.putExtra(TOTAL_DONE, Total);// Start mainActivity.startActivity(beginPushup);}TextUtils是Android Framework中提供的一个类。这将检查内容是否为空且是否仅为数字。如果您确定只有数字是您的编辑文本的输入,您显然可以省略数字检查。

九州编程

因为当您的 EditText 为空时,它没有数值。你可以做Integer.parseInt(insertcountBtn.getText().toString());当输入为空白时;那里没有 Integer 值,因为什么都没有,所以它会抛出一个 NumberFormatException。您可以执行以下操作以确保它具有值(失败时默认值为 0):int insertedcountInt;try {&nbsp; &nbsp; insertedCountInt = Integer.parseInt(insertcountBtn.getText().toString());} catch (NumberFormatException e) {&nbsp; &nbsp; insertedCountInt = 0;}

三国纷争

这里的问题是您正在定义 editText 以仅检查整数:您没有为输入字符串等情况放置条件语句,如果 editText 为空,则它包含字符串。因此,您可能想要放置类似的东西;String editTextString = String.valueOf(insertcountBtn.getText());if (editTextString == "") {&nbsp; &nbsp; //do something}或者,String editTextString = insertcountBtn.getText().toString();if (editTextString == "") {//do something}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java