我正在尝试在我的某个应用程序片段中的getView()方法中为新视图充气。为此,我创建一个LayoutInflater,然后膨胀convertView:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.inflater_my_search, parent);
}
return convertView;
}
然而,当我这样做时,我得到了这个例外:
android.view.InflateException:二进制XML文件行#101:AdapterView不支持addView(View,LayoutParams)
是什么导致了这个问题?
答案 0 :(得分:2)
使用inflater.inflate(R.layout.inflater_my_search, parent, false);
2参数方法试图将视图附加到父级,抛出异常
答案 1 :(得分:0)
convertView = inflater.inflate(R.layout.inflater_my_search, parent); // here
your trying to add the view in parent make sure you remove all views and then add.
而不是使用简单的技术,如
1. convertView = inflater.inflate(R.layout.inflater_my_search, parent, false);//
here your inflating view but not adding the parent.(false flag)
2 convertView = inflater.inflate(R.layout.inflater_my_search, null)//
here your not supplying any parent.