1

I am getting ClassCastException when i try to add HeaderView to ListView here is the code of HeaderView

 View headerView = View.inflate(this, R.layout.header, null);
 listview.addHeaderView(headerView);
 listview.setAdapter(adapter); 

and here is the stackTrace

04-25 12:38:42.324: E/AndroidRuntime(18494): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.widget.ListView.setupChild(ListView.java:1826)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.widget.ListView.makeAndAddView(ListView.java:1793)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.widget.ListView.fillDown(ListView.java:691)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.widget.ListView.fillFromTop(ListView.java:752)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.widget.ListView.layoutChildren(ListView.java:1630)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.widget.AbsListView.onLayout(AbsListView.java:2091)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.view.View.layout(View.java:14817)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.view.View.layout(View.java:14817)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.widget.ScrollView.onLayout(ScrollView.java:1468)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.view.View.layout(View.java:14817)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.view.View.layout(View.java:14817)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.view.View.layout(View.java:14817)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.view.View.layout(View.java:14817)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.view.View.layout(View.java:14817)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.view.Choreographer.doFrame(Choreographer.java:544)
04-25 12:38:42.324: E/AndroidRuntime(18494):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)

EDIT:

This is my Layout

<RelativeLayout
            android:id="@+id/re3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent" >


        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            android:cacheColorHint="#000000"
             >
        </ListView>
</RelativeLayout>

and this is my Activity code

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.lay);

    ListView listview=(ListView) findViewById(R.id.list);
    adapter = new LazyAdapter(this,l, list_cons);

    View headerView = View.inflate(this, R.layout.header, null);

    listview.addHeaderView(headerView);
    listview.setAdapter(adapter);
}
6
  • Can you post more of your codes ? Commented Apr 25, 2014 at 7:47
  • what should I post more? Commented Apr 25, 2014 at 7:48
  • @user3294034 what you want to do with the code, post your this much code that we can understand it and help you out. Commented Apr 25, 2014 at 7:51
  • Post Activity and layout files. Commented Apr 25, 2014 at 7:51
  • I want to add a header to the top of ListView this is my need. Commented Apr 25, 2014 at 7:54

2 Answers 2

6

UPDATE: as @Zielony pointed out, the previous solution would attach your header to your list as a child, and you don't want that. Here is what you can do to inflate properly your layout_* attributes without attaching the header:

View header = LayoutInflater.from(getContext())
                        .inflate(R.layout.list_header, listView, false);
listView.addHeaderView(header);

Try to specify the parent for the view you inflate. Replace this:

View headerView = View.inflate(this, R.layout.header, null);

By this:

View headerView = View.inflate(this, R.layout.header, listview);

This should set the LayoutParams to the right type, not causing the ClassCastException anymore.

Sign up to request clarification or add additional context in comments.

3 Comments

this will also add header to the ListView as a child. ListView uses special type of adapter for lists with headers and footers
You sure of that? I think the ViewGroup parameter is here only for inflation purposes, to properly determine the layout_* parameters type.
-1
View header = (View)getLayoutInflater().inflate(R.layout.headerView,null);
ls.addHeaderView(header);

In my case, this works

View header = getLayoutInflater().inflate(R.layout.header, null);
View footer = getLayoutInflater().inflate(R.layout.footer, null);
ListView listView = getListView();
listView.addHeaderView(header);
listView.addFooterView(footer);
setListAdapter(new
ArrayAdapter<String(this,android.R.layout.simple_list_item_single_choice,android.R.id.text1, names)); 

Comments

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.