简体   繁体   中英

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

I've got a [DONE] button that's supposed to check 2 entries's results. One of them is an [EditText--inputType:number] and the other is a [TextView] that increments when a certain button is pressed.

What I'm trying to do is check whether the EditText has an integer or is null, and check the contents of the TextView. if they both are greater than Zero. I add them up and send the total to my main activity. Here's the code i have so far.

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

}

The problem i'm having is with either textView or EditText i'm not sure. All i know is that if i fill them both and click done it adds them up and transfers the total to mainActivity as expected. If I add values to EditText and leave TextView with 0 it also does what it's meant to do. But if I increment TextView and leave EditText blank, it does not transfer my TextView integer and crashes app.

Could i be detecting the editText wrong, because i think that's the reason. If so what's the right way?

----- First && Second Answer Edits -----

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

Good news is no more crash, but unless I fill the EditText with something, I'm not receiving my Integer in mainActivity. This is getting interesting. * I think it's a play of the if statements which I've restructures, but no luck so far. *

----- Updated Working Code For Any Future Requests -----

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 As you can see I ended up dividing the if-logic from the Intent to Transfer to main Activity. This way they're both simple. And using the Do Not Repeat Yourself i did the same thing to the rest of my other buttons as you can see with all the if's in [returnbtn] method. I also simplified the if statement aside from the ones i was helped with. I ended up needing 2 if statements to manage getting a total from the 2 entries. Thanks again for the help everyone. Ohh the try-except didn't seem necessary so i deprecated them. as the app gets more complex i'll add them if necessary.

Because when your EditText is empty, it has no numerical value.

You can do

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

when the input is blank; there's no Integer value there, because there's nothing at all, so it's going to throw a NumberFormatException.

You can do the following to make sure it has a value (default will be 0 on failure):

int insertedcountInt;

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

The issue here is you are defining editText to check for only interger: you did not put the conditional statement for the cases like if String was entered, if editText is blank, it contains String. Hence, you may want to put something like;

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

Alternatively,

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

As the other answer explained, the issue with your code is that when the EditText is blank then parsing "null" is causing exception. So you just have to insure that if content is null then just use 0(Zero) value.
You can try this:

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 is a class provided in Android Framework.
This will check for if the content is not empty and also digits only. You can obviously omit the digit check if you are sure that only number will be the input of your edit text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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