当我尝试将ClassCastException
添加到HeaderView
时,我收到ListView
这里的代码为HeaderView
View headerView = View.inflate(this, R.layout.header, null);
listview.addHeaderView(headerView);
listview.setAdapter(adapter);
这是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)
修改
这是我的布局
<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>
这是我的活动代码
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);
}
答案 0 :(得分:6)
更新:正如@Zielony指出的那样,之前的解决方案会将您的标题作为孩子附加到您的列表中,而您却不希望如此。您可以采取以下措施,在不附加标题的情况下正确填充layout_*
属性:
View header = LayoutInflater.from(getContext())
.inflate(R.layout.list_header, listView, false);
listView.addHeaderView(header);
尝试为您要充气的视图指定父级。替换这个:
View headerView = View.inflate(this, R.layout.header, null);
由此:
View headerView = View.inflate(this, R.layout.header, listview);
这应该将LayoutParams设置为正确的类型,而不再导致ClassCastException
。
答案 1 :(得分:-1)
View header = (View)getLayoutInflater().inflate(R.layout.headerView,null);
ls.addHeaderView(header);
就我而言,这是有效的
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));