JTM JTM - 1 month ago 10
Java Question

Why does inflating a view in getView() cause an exception?

I am trying to inflate a new view in the getView() method in one of my app's fragments. To do this, I create an LayoutInflater, and then inflate 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;
}


However, when I do this, I get this exception:


android.view.InflateException: Binary XML file line #101: addView(View, LayoutParams) is not supported in AdapterView


What is causing this problem?

Answer

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

The 2 param method is trying to attach the view to the parent, throwing the Exception