I have a ListView
with set onItemClickListener
:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// not important
if (!found) {
activity.addSelectedIngredient(ingred);
parent.getChildAt(position).setBackgroundColor(Color.parseColor("#ff99FE80"));
} else {
activity.removeSelectedIngredient(ingred);
parent.getChildAt(position).setBackgroundColor(Color.WHITE);
}
}
The NullPointerException
is thrown when parent hasn't got child on selected position (e.g. 15). Why? How it's possible that the element might not be present if she already selected it?
Edit:
if (!found) {
activity.addSelectedIngredient(ingred);
view.setBackgroundColor(Color.parseColor("#ff99FE80"));
} else {
activity.removeSelectedIngredient(ingred);
view.setBackgroundColor(Color.WHITE);
}