1

This code generates a new EditText each time the button is pressed

final RelativeLayout layout = (RelativeLayout) findViewById(R.id.ataque);

mas.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        EditText extra = new EditText(getApplicationContext());
        int ntext = mispreferencias.getInt("contadortext", 0);
        RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        extra.setId(ntext);
        final int id_ = extra.getId();
        extra.setLayoutParams(p);
        editor.putInt("extra" + ntext, id_);
        editor.putInt("contadortext", ntext + 1);
        editor.commit();
        layout.addView(extra);
   }
});

With them already generated, I try to get the text that is written in them and save it in a SharedPreferences, but it does not work.

int ntext = mispreferencias.getInt("contadortext", 0);

for (int i = 0; i < ntext; i++) {
    int ide = mispreferencias.getInt("extra" + i, 0);
    EditText edi = findViewById(ide);
    editor.putString("text" + i, edi.getText().toString());
}

It returns the error:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference

2 Answers2

0

A quick look at the definition of the findViewById method gives us this

@Nullable
public <T extends View> T findViewById(@IdRes int id) {
    return getWindow().findViewById(id);
}

Another quick look at the definition of @IdRes tells us this

Denotes that an integer parameter, field or method return value is expected to be an id resource reference

Now we know that findViewById expects an Integer referencing a Resource.

We also know that R can't be modified during runtime.

We can conclude that you can't use findViewById for Views you have programatically created during runtime.

You could, but I do NOT recoment this, create a XML containing IDs as indicated in this link

0

Since you want to iterate through all the EditTexts and save their texts, why don't you store them at creation in a list? So you avoid all the id trouble.

ArrayList<EditText> list = new ArrayList();
final RelativeLayout layout = (RelativeLayout) findViewById(R.id.ataque);

mas.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        EditText extra = new EditText(getApplicationContext());
        RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        list.add(extra);
        layout.addView(extra);
   }
});

now iterate through the list:

for (int i = 0; i < list.size(); i++) {
    editor.putString("text" + i, list.get(i).getText().toString());
}
forpas
  • 160,666
  • 10
  • 38
  • 76
  • I agree with your comment, but I think saving the `EditText` objects into a `Map` would suit him better, seeing as he is trying to retrieve them by ID. – Mauro Curbelo Dec 15 '18 at 23:00
  • Actually he does not need the id if the only he needs is store the texts in SharedPreferences. After all by having all the EditTexts in a list, he can access them by index, as he created them. – forpas Dec 15 '18 at 23:10