正文
一、問題代碼
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;
}
代碼說明:
a). LayoutParams是指android.view.ViewGroup.LayoutParams。
b). 程式運作後将出錯,錯誤很莫名其妙且不固定。
二、問題分析
經調試發現在擷取LayoutParams時内部是将這個屬性轉到android.widget.AbsListView.LayoutParams,也就是這裡setLayoutParams弄錯了,這裡正确的方法是使用GridView.LayoutParams,替換之後問題得以解決。
本文轉自over140 51CTO部落格,原文連結:http://blog.51cto.com/over140/582224,如需轉載請自行聯系原作者