Aldridge1991
Aldridge1991

Reputation: 1367

Adding strings to stringArray android

i've problem when adding string to a stringArray. It crashes every time:

ArrayList<String> newString, limits;

    String pruebas;     
    pruebas=e1.getText().toString();
    if (pruebas == null || pruebas.equals("")) {
        Toast.makeText(Limits.this, "You did not enter a number", Toast.LENGTH_SHORT).show();
        return;
    }
    else{
        limits.add(pruebas);
    }

Any help would be appreciated!

Thankyou

Upvotes: 0

Views: 22235

Answers (2)

Dipak Keshariya
Dipak Keshariya

Reputation: 22291

Please Use below code, it will solve your problem.

Code for Add String into ArrayList

ArrayList<String> limits = new ArrayList<String>();

String pruebas=e1.getText().toString();

if (pruebas == null || pruebas.equals("")) {
    Toast.makeText(Limits.this, "You did not enter a number", Toast.LENGTH_SHORT).show();
    return;
}else {
    limits.add(pruebas);
}

Code for Add String into String Array, Here 10 is size of String Array and index is position of string array which you want to store the string value.

String[] limits = new String[10];

String pruebas=e1.getText().toString();

if (pruebas == null || pruebas.equals("")) {
    Toast.makeText(Limits.this, "You did not enter a number", Toast.LENGTH_SHORT).show();
    return;
}else {
    limits[index] = pruebas;
}

Upvotes: 5

Sanket Kachhela
Sanket Kachhela

Reputation: 10856

You have not intialize ArrayList... so use this below

   ArrayList<String> newString, limits;

   limits = new ArrayList<String>();

   newString = new ArrayList<String>();

    String pruebas;     
    pruebas=e1.getText().toString();
    if (pruebas == null || pruebas.equals("")) {
        Toast.makeText(Limits.this, "You did not enter a number", Toast.LENGTH_SHORT).show();
        return;
    }
    else{
        limits.add(pruebas);
    }

Upvotes: 2

Related Questions