声明
欢迎转载,但请保留文章原始出处:)
农民伯伯: http://over140.blog.51cto.com/
正文
一、问题代码
public
View getView(
int
position, View convertView, ViewGroup parent) {
if (convertView == null ) {
TextView item = new TextView(context);
item.setText( " Test " );
item.setLayoutParams( new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
return item;
} else
return convertView;
}
if (convertView == null ) {
TextView item = new TextView(context);
item.setText( " Test " );
item.setLayoutParams( new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
return item;
} else
return convertView;
}
代码说明:
a). LayoutParams是指android.view.ViewGroup.LayoutParams。
b). 程序运行后将出错,错误很莫名其妙且不固定。
二、问题分析
经调试发现在获取LayoutParams时内部是将这个属性转到android.widget.AbsListView.LayoutParams,也就是这里setLayoutParams弄错了,这里正确的方法是使用GridView.LayoutParams,替换之后问题得以解决。
结束
之前一直是复制、粘贴这部分的代码,所以没注意有这问题,这里记录一下。
转载于:https://blog.51cto.com/over140/582224