user3092616
user3092616

Reputation: 19

ClassCastException adding header to list view (ListFragment onCreateView)

I have a listview with a custom adapter, it renders and works fine. The moment I try to add a header to the listview the app crashes. My header is a seperate layout xml file it has a relative layout with two spinners one on the left and one on the right.

I inflate the view, manipulate the spinners to add data, add it to the list view and the app no longer works when the view is created it crashes.

Java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams.

I am using a Fragment that extends list fragment and the xml layout just has a list in it. The code used to add the header is in the onCreateView call,

        View view = inflater.inflate(R.layout.listview_layout, container, false);

        View header = (View)inflater.inflate(R.layout.header_layout, container, false);
        Spinner spinner1 = (Spinner)header.findViewById( R.id.spinner1 );

        //add pre-defined per page information
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                getActivity(),
                R.array.header_layout_spinner1, 
                android.R.layout.simple_spinner_item
        );
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter( adapter );

        Integer values[] = new Integer[100];
        for(int i=1;i<=100;i++)
        {
            pages[i-1] = i;
        }

        Spinner spinner2 = (Spinner)header.findViewById(R.id.spinner2); 
        ArrayAdapter<Integer> adapter2 = new ArrayAdapter<Integer>(getActivity(), android.R.layout.simple_spinner_item, values);
        spinner2.setAdapter(adapter2);

        //include header in the view
        ((ListView)view.findViewById(R.id.list)).addHeaderView(header);

Upvotes: 0

Views: 148

Answers (1)

iMDroid
iMDroid

Reputation: 2128

Try changing the line...

View header = (View)inflater.inflate(R.layout.header_layout, container, false);

to

View header = (View)inflater.inflate(R.layout.header_layout, null, false);

Upvotes: 1

Related Questions