我正在創建包含多個元素的自定義ListView
- TextView
和ImageView
。將圖像資源動態設置爲列表項
這裏是我的getView
方法的一部分:
@override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.individual_items_in_options, parent, false);
}
// Find the option to start with.
Options currentOption = optionsOfAQuestion.get(position);
// option number
TextView optionNumber = (TextView) itemView.findViewById(R.id.option_number);
optionNumber.setText(currentOption.getOptionNumber());
// option content
TextView optionContent = (TextView) itemView.findViewById(R.id.option_content);
optionContent.setText(currentOption.getOptionContent());
ImageView imageView = (ImageView)findViewById(R.id.option_icon);
imageView.setImageResource(R.drawable.icon_wrong);
我越來越NPE在設置圖像資源,雖然我已經在我的佈局中定義它。爲什麼設置圖像給我NPE,而設置TextViews的工作完全正常?我在這裏錯過了什麼?
你錯過了'itemView'調用' findViewById' – camelCaseCoder