0

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);
2
  • 2
    Please post the crash stack, and be sure to refer to which line of your posted code is referred to in the stack. Commented Nov 26, 2014 at 3:36
  • This is the first line following null pointer exception from the crash stack, the remainder is trivial because that line is the problem causing all other lines. Commented Nov 26, 2014 at 15:24

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Stops it from crashing, but won't visually display as I want it to. It won't show this view at the top of the listview in the listfragment...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.