【问题标题】:Why are the integers being sent from one android app Activity to another causing my app to crash?为什么整数从一个 android 应用程序 Activity 发送到另一个导致我的应用程序崩溃?
【发布时间】:2019-02-21 16:07:04
【问题描述】:

我有一个 [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 错误吗,因为我认为这就是原因。 如果是这样,正确的方法是什么?

----- 第一个 && 第二个答案编辑 -----

    int insertcountInt;
    String insertcountString = 
    String.valueOf(insertcountBtn.getText());

    try {
        insertcountInt = 
    Integer.parseInt(insertcountBtn.getText().toString());

        if (insertcountInt <= 0 || insertcountString == " ") Total = 
    givencountInt;
        if (insertcountInt > 0 ) Total = givencountInt + 
    insertcountInt;

    } catch (NumberFormatException e) {
        insertcountInt = 0;
    }

好消息是不再崩溃,但除非我用一些东西填充 EditText,否则我不会在 mainActivity 中收到我的 Integer。这越来越有趣了。 * 我认为这是我已经重组的 if 语句的游戏,但到目前为止还没有运气。 *

----- 更新了任何未来请求的工作代码 -----

public void filledChecker() {

    // Initialize insert textView
    EditText insertcountBtn = 
    findViewById(R.id.insertPushup);

    // Initialize counter textView
    TextView givencountBtn = findViewById(R.id.showCount);

    int insertcountInt = 0;
    int givencountInt = 0;

    // get added int stuff from the insert textField
    if (!TextUtils.isEmpty(insertcountBtn.getText()) && 
    TextUtils.isDigitsOnly(insertcountBtn.getText())) {
        insertcountInt = 
    Integer.parseInt(insertcountBtn.getText().toString());
    }

    // get string stuff from counter textView
    String givencountString = givencountBtn.getText().toString();
    if (!TextUtils.isEmpty(givencountString) && 
    TextUtils.isDigitsOnly(givencountString)) {
        givencountInt = Integer.parseInt(givencountString);
    }

    if (insertcountInt <= 0) {
        Total = givencountInt;
    }
    if (insertcountInt > 0) {
        Total = givencountInt + insertcountInt;
    }

}

public void returnbtn(View view) {
    // Create an Intent to return to the mainActivity.
    Intent beginPushup = new Intent(this, MainActivity.class);
    filledChecker();
    Integer current_Id = getIntent().getIntExtra(GIVEN_ID, 0);

    // Pass the current number to the push-up Counter activity.
    if (current_Id == 1) beginPushup.putExtra(TOTAL_P_DONE,     
    Total);
    if (current_Id == 2) beginPushup.putExtra(TOTAL_S_DONE,     
    Total);
    if (current_Id == 3) beginPushup.putExtra(TOTAL_C_DONE,     
    Total);
    if (current_Id == 4) beginPushup.putExtra(TOTAL_SQ_DONE,    
    Total);
    beginPushup.putExtra(GIVEN_ID, current_Id);

    // Start mainActivity.
    startActivity(beginPushup);
}

I 如您所见,我最终将 if 逻辑从 Intent 转移到主 Activity。这样他们都很简单。并且使用 Do Not Repeat Yourself 我对其他按钮做了同样的事情,正如您在 [returnbtn] 方法中看到的所有 if 一样。除了我得到帮助的那些之外,我还简化了 if 语句。我最终需要 2 个 if 语句来管理从 2 个条目中获取总数。再次感谢大家的帮助。哦,try-except 似乎没有必要,所以我弃用了它们。随着应用变得越来越复杂,我会在必要时添加它们。

【问题讨论】:

  • 你能发布堆栈跟踪吗?
  • 代码完全没问题。当我在我的虚拟设备上运行它时发生崩溃。这是否准确地回答了您的问题?或者还有其他你想看的代码。我不确定在哪里可以找到此类崩溃的错误日志。
  • 可以这么说,我所有的 gradle 构建都完全没有错误。 :D

标签: java android android-intent integer


【解决方案1】:

正如另一个答案所解释的,您的代码的问题是,当 EditText 为空白时,解析“null”会导致异常。因此,您只需确保如果内容为空,则只需使用 0(零)值。
您可以试试这个:

public void returnbtn(View view) {

// Initialize insert textView
EditText insertcountBtn = findViewById(R.id.insertPushup);

// Initialize counter textView
TextView givencountBtn = findViewById(R.id.showCount);

int insertcountInt = 0;
int givencountInt = 0;

// get added int stuff from the insert textField
if (!TextUtils.isEmpty(insertcountBtn.getText()) && TextUtils.isDigitsOnly(insertcountBtn.getText())) {
    insertcountInt = Integer.parseInt(insertcountBtn.getText().toString());
}

// get string stuff from counter textView
String givencountString = givencountBtn.getText().toString();
if (!TextUtils.isEmpty(givencountString) && TextUtils.isDigitsOnly(givencountString)) {
    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);
}


TextUtils是Android Framework中提供的一个类。
这将检查内容是否不为空,也只检查数字。如果您确定只有数字将是您的编辑文本的输入,您显然可以省略数字检查。

【讨论】:

  • 我真的很喜欢你的回答,因为它有效:D。但只是一个问题,如果您没有使用 TextUtils,您是否能够获得相同的结果。就我个人而言,我不知道 TextUtils 存在,所以我现在正在阅读它。我只是想知道如果没有它,是否有人能够为 EditText 分配一个默认整数?顺便说一句,感谢您的帮助。你救了我很多头抓伤。 :D
  • 是的,你可以在没有 TextUtils 的情况下达到同样的效果。首先,正如我在代码中所做的那样,我们为这些变量分配一个默认值“0”。然后我们需要像这样检查 null - if (insertcountBtn.getText() != null && insertcountBtn.getText().toString() != null)。因此,如果条件成功,我们会解析数字。对 givencountBtn 也可以这样做。要检查文本是否只有数字 - 您需要编写自己的方法,该方法将遍历字符串的每个字符以确保它只有数字
【解决方案2】:

这里的问题是您将 editText 定义为仅检查整数:您没有为输入 String 等情况放置条件语句,如果 editText 为空,则它包含 String。因此,您可能想要放置类似的东西;

String editTextString = String.valueOf(insertcountBtn.getText());
if (editTextString == "") {
    //do something
}

或者,

String editTextString = insertcountBtn.getText().toString();
if (editTextString == "") {
//do something
}

【讨论】:

  • 很好的建议。我更新了我的帖子,包括使用空字符串“”检测,仍然没有运气。
  • 我刚刚重读了您的帖子,您应该知道的是,在 xml 中,我认为它是数字的 inputType。 android:inputType="number" 。所以它是空的,但不是字符串,这会被归类为null吗??
  • 即使输入类型是整数,如果你想得到editText文本,你总是会得到它的字符串通知。因此,只要它为空,您就可以引用它有一个空字符串
  • 好吧,让我给你一个用例,通过检查 if (editTextString != "") { // 在这里执行所有 if 语句}
  • 因此,如果editText为空,则不会执行任何代码
【解决方案3】:

因为当你的 EditText 为空时,它没有数值。

你可以的

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

当输入为空时;那里没有 Integer 值,因为什么都没有,所以会抛出 NumberFormatException。

您可以执行以下操作以确保它有一个值(失败时默认为 0):

int insertedcountInt;

try {
    insertedCountInt = Integer.parseInt(insertcountBtn.getText().toString());
} catch (NumberFormatException e) {
    insertedCountInt = 0;
}

【讨论】:

  • @blaisejohnny 我没有建议。我建议尝试捕捉。
  • 我使用 try and catch 编辑了我的帖子。我看到的一件事是 [Catch] 的内容,即 insertcounted = 0;被认为是多余的,因为它从未被使用过。这引出了一个问题,它实际上是默认为 0 还是等待崩溃的代码将其分配为 0?这不是我想要的。如果有一个库可以获取 TextView 的内容并同时为其分配一个默认值,我认为它会有点像getIntent.getIntExtra(String, default int) 的工作方式——到这一行insertcountInt = Integer.parseInt(insertcountBtn.getText().toString());
  • 没有类似的东西,因为 EditTexts 不是这样工作的。您可以直接分配变量。但是您的代码没有按照我的建议执行。 只有变量的赋值应该在try 块中。其他一切都在它之下和之外。
  • 哦,好的。我尝试了第一件事,没有运气。我可能做得不好。感谢您的建议。我很感激。
  • 您还需要对 givenCountInt 应用相同的逻辑。你能发布你更新的代码吗?
猜你喜欢
  • 1970-01-01
  • 2014-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多