2016-11-04 69 views
0

我想在我的应用程序的片段中的一个getView()方法中膨胀新视图。要做到这一点,我创建一个LayoutInflater,然后膨胀convertView:为什么膨胀getView()中的视图会导致异常?

@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

convertView =充气.inflate(R.layout.inflater_my_search,parent);它是造成,只是做convertView = inflater.inflate(R.layout.inflater_my_search,null); – dex

回答

2

使用inflater.inflate(R.layout.inflater_my_search, parent, false);

的2 PARAM方法试图视图附加到母体,抛出异常

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.