0
我有自定義列表視圖,我正在使用自定義適配器。當應用程序打開時,前幾項顯示正確。然而,當我開始向下滾動,我在getView()方法得到一個NullPointerException異常:滾動時自定義ListView getView()方法nullPointerException
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ListViewItem item = items.get(position);
View vi = convertView;
if (item.Type.equals("Homework")) {
if (convertView == null)
vi = inflater.inflate(R.layout.item_row_homework, null);
ImageView imgThumbnail = (ImageView) vi
.findViewById(R.id.homework_imgThumbnail);
TextView txtTitle = (TextView) vi
.findViewById(R.id.homework_txtTitle);
TextView txtSubTitle = (TextView) vi
.findViewById(R.id.homework_txtSubTitle);
imgThumbnail.setImageResource(item.ThumbnailResource);
txtTitle.setText(item.Title);
txtSubTitle.setText(item.SubTitle);
}
if (item.Type.equals("Study")) {
if (convertView == null)
vi = inflater.inflate(R.layout.item_row_study, null);
ImageView imgThumbnail = (ImageView) vi
.findViewById(R.id.study_imgThumbnail);
TextView txtTitle = (TextView) vi
.findViewById(R.id.study_txtTitle);
TextView txtSubTitle = (TextView) vi
.findViewById(R.id.study_txtSubTitle);
imgThumbnail.setImageResource(item.ThumbnailResource);
txtTitle.setText(item.Title);
txtSubTitle.setText(item.SubTitle);
}
if (item.Type.equals("Project")) {
if (convertView == null)
vi = inflater.inflate(R.layout.item_row_project, null);
ImageView imgThumbnail = (ImageView) vi
.findViewById(R.id.project_imgThumbnail);
TextView txtTitle = (TextView) vi
.findViewById(R.id.project_txtTitle);
TextView txtSubTitle = (TextView) vi
.findViewById(R.id.project_txtSubTitle);
imgThumbnail.setImageResource(item.ThumbnailResource);
txtTitle.setText(item.Title);
txtSubTitle.setText(item.SubTitle);
}
return vi;
}
和它有任何
imgThumbnail.setImageResource(item.ThumbnailResource);
我已經看過該行的三個實例其他關於此問題的SO問題,但尚未明確究竟發生了什麼。
謝謝。
您是否在'R.id.homework_imgThumbnail','R.id.study_imgThumbnail'和'R.id.project_imgThumbnail'不會返回null?時仔細檢查'findViewById'。 – rbarriuso
發佈您的logcat。檢查logcat告訴你哪裏有一個NPE。 –
@ epsilondelta你可以請檢查問題是否得到解答? – rbarriuso