当我有一个ListView
与集onItemClickListener
:NullPointerException异常点击ListView项
@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);
}
}
的NullPointerException
当父母还没有得到孩子在选定的位置(例如15)被抛出。为什么?如果元素可能不存在,如果她已经选择了它?
编辑:
if (!found) {
activity.addSelectedIngredient(ingred);
view.setBackgroundColor(Color.parseColor("#ff99FE80"));
} else {
activity.removeSelectedIngredient(ingred);
view.setBackgroundColor(Color.WHITE);
}
什么是'found'?当它不应该是报告错误吗?此外,尽可能多的诙谐的标题很有趣,它们是误导和无益的 – Grambot
当发现选定的项目已被点击时'found'为'true',我必须将其背景变回白色。对不起,标题,但我忍不住发布它;) –