gpt4 book ai didi

Android: ClassCastException 添加 HeaderView 到 Listview

转载 作者:行者123 更新时间:2023-11-29 15:16:59 26 4
gpt4 key购买 nike

我得到 ClassCastException当我尝试添加 HeaderViewListView这是 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>

这是我的 Activity 代码

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);
}

最佳答案

更新:正如@Zielony 指出的那样,以前的解决方案会将您的 header 作为子项附加到您的列表中,而您不希望这样。以下是您可以在不附加 header 的情况下正确扩展 layout_* 属性的方法:

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

尝试为您膨胀的 View 指定父 View 。替换为:

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

由此:

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

这应该将 LayoutParams 设置为正确的类型,不再导致 ClassCastException

关于Android: ClassCastException 添加 HeaderView 到 Listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23287138/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com