I have some TextView which set the text from value saved in SharedPreferences
. The values are split as array using "@".
SharedPreferences mark = getSharedPreferences(PREFS_NAME, 0);
String savestock = mark.getString("stock", null);
if (savestock != null) {
stock = savestock.split("@", -1);
tvstrength.setText(stock[0]);
tvavailable.setText(stock[1]);
tvbrand.setText(stock[2]);
tvbatch.setText(stock[3]);
tvexp.setText(stock[4]);
tvstrength2.setText(stock[5]);
tvavailable2.setText(stock[6]);
tvbrand2.setText(stock[7]);
tvbatch2.setText(stock[8]);
tvexp2.setText(stock[9]);
}
This is how the SharedPreferences
is saved:
SharedPreferences mark = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = mark.edit();
editor.putString("stock", inStrength + "@" + inamt + "@" + inBrand + "@" + inBatch + "@" + inExp +
inStrength2 + "@" + inamt2 + "@" + inBrand2 + "@" + inBatch2 + "@" + inExp2);
editor.commit();
However, I got the error java.lang.ArrayIndexOutOfBoundsException: length=9; index=9
. The error is caused by tvexp2.setText(stock[9]);
What mistake I made?