繁体   English   中英

为什么整数从一个android应用活动发送到另一个,导致我的应用崩溃?

[英]Why are the integers being sent from one android app Activity to another causing my app to crash?

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

我想做的是检查EditText是否为整数或为null,然后检查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);
}

正如您所看到的,我最终将if-logic从转移到主Activity的意图中划分出来。 这样,它们都很简单。 使用“请勿重复自己”,我对其余的其他按钮做了同样的事情,就像在[returnbtn]方法中使用的所有if一样。 除了我得到帮助的内容之外,我还简化了if语句。 我最终需要2个if语句来管理从2个条目中获取的总计。 再次感谢大家的帮助。 哦,试一试似乎没有必要,所以我弃用了它们。 随着应用变得越来越复杂,我将在必要时添加它们。

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

你可以做

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

输入为空时; 那里没有Integer值,因为什么都没有,所以它将引发NumberFormatException。

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

int insertedcountInt;

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

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

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

或者,

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

正如另一个答案所解释的那样,您的代码存在的问题是,当EditText为空白时,解析“ null”会导致异常。 因此,您只需要确保如果content为null,则只需使用0(Zero)值即可。
您可以尝试以下方法:

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中提供的类。
这将检查内容是否不为空,也只能为数字。 如果您确定只有数字将作为编辑文本的输入,则可以明显地省略数字检查。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM