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