【发布时间】:2015-01-22 00:33:56
【问题描述】:
我的一个片段中有一个列表视图,直到今天我将其高度设置为固定 400dp。现在我将其更改为 0dp 并使用布局中的其他视图和按钮对其进行加权。但是,在进行该更改后,我开始获得空指针。显然,当为列表中的项目调用 getView() 时,尝试使用 view.findViewById() 获取诸如 textview 之类的资源会突然返回 null。仅当滚动条可见并且我尝试向下滚动时才会发生这种情况。
在做了一些研究之后,我注意到每当从被移动的滚动条触发 getView() 时,视图都会设置为 convertView。出于某种原因,convertView 实际上是一个 textView?为什么这会作为 convertView 传递?为什么这在固定高度下工作,我该如何解决这个问题?
@Override
public View getView(LayoutInflater inflater, View convertView) {
View view;
if (convertView == null) {
view = (View) inflater.inflate(R.layout.interview_item, null);
// Do some initialization
} else {
view = convertView;
}
//Crash on the next line with an error that text1 is a null pointer
TextView text1 = (TextView) view.findViewById(R.id.name);
TextView text2 = (TextView) view.findViewById(R.id.job);
TextView text3 = (TextView) view.findViewById(R.id.time);
ImageButton image = (ImageButton) view.findViewById(R.id.interview_type);
text1.setText(this.getCandidate().getName());
text2.setText(this.getJob().getTitle());
//Parse the time of day from the date and format it
SimpleDateFormat timeFmtOut = new SimpleDateFormat("h:mm a");
text3.setText(timeFmtOut.format(this.getDate() ));
if(this.type == interviewType.PHONE_CALL)
image.setImageResource(R.drawable.phone_interview);
else
image.setImageResource(R.drawable.person_interview);
return view;
}
thanks
【问题讨论】:
-
你能给你的
R.layout.interview_item吗?