2016-11-04 72 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.