Android:向 ExpandableListView 添加标头视图时出现 ClassCastException
我正在尝试向 ExpandableListView 添加标头,如下所示:
headerView = View.inflate(this, R.layout.header, null);
expandableListView.addHeaderView(headerView);
expandableListView.setAdapter(new SectionedAdapter(this));
这给了我以下错误:
12-08 16:23:42.354:
ERROR/AndroidRuntime(421): Caused by:java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
12-08 16:23:42.354: ERROR/AndroidRuntime(421): at android.widget.ListView.clearRecycledState(ListView.java:504)
12-08 16:23:42.354: ERROR/AndroidRuntime(421): at android.widget.ListView.resetList(ListView.java:490)
12-08 16:23:42.354:ERROR/AndroidRuntime(421):at android.widget.ListView.setAdapter(ListView.java:422)
12-08 16:23:42.354:ERROR/AndroidRuntime(421): at android.widget.ExpandableListView.setAdapter(ExpandableListView.java:475)
这是在调用 expandableListView.setAdapter(new SectionedAdapter(this))
时发生的,但我可以'不明白为什么。有什么想法吗?
I'm trying to add a header to an ExpandableListView like so:
headerView = View.inflate(this, R.layout.header, null);
expandableListView.addHeaderView(headerView);
expandableListView.setAdapter(new SectionedAdapter(this));
Which gives me the following error:
12-08 16:23:42.354:
ERROR/AndroidRuntime(421): Caused by:java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
12-08 16:23:42.354: ERROR/AndroidRuntime(421): at android.widget.ListView.clearRecycledState(ListView.java:504)
12-08 16:23:42.354: ERROR/AndroidRuntime(421): at android.widget.ListView.resetList(ListView.java:490)
12-08 16:23:42.354:ERROR/AndroidRuntime(421):at android.widget.ListView.setAdapter(ListView.java:422)
12-08 16:23:42.354:ERROR/AndroidRuntime(421): at android.widget.ExpandableListView.setAdapter(ExpandableListView.java:475)
This is happening at the call to expandableListView.setAdapter(new SectionedAdapter(this))
, but I can't figure out why. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,我想出了这个。我通过以编程方式将视图的 LayoutParams 设置为 ListView LayoutParams 摆脱了运行时错误,如下所示:
在添加视图之前。原因可以在 Android 文档中找到:
http: //developer.android.com/reference/android/view/View.html#setLayoutParams(android.view.ViewGroup.LayoutParams)
其中指出:
所以基本上,如果您将一个视图添加到另一个视图,则必须将视图的 LayoutParams 设置为父级使用的 LayoutParams 类型,否则您将收到运行时错误。
Ok, I figured this one out. I got rid of the runtime error by programatically setting the View's LayoutParams to ListView LayoutParams, like so:
before adding the view. The reason being is found in the Android docs:
http://developer.android.com/reference/android/view/View.html#setLayoutParams(android.view.ViewGroup.LayoutParams)
which states that:
So basically, if you are adding a view to another, you MUST set the LayoutParams of the view to the LayoutParams type that the parent uses, or you will get a runtime error.
尽管上述答案可能对许多人有用,但仍然有更好的解释。
ClassCastException
是调用listview.addHeaderView
或listview.addFooterView
后的正常行为。原因是初始适配器包装在 HeaderViewListAdapter 中。
要获取您的适配器,请使用此代码。
代码取自此处
Even though the above answer might work for many, there is still a better explanation.
ClassCastException
is the normal behaviour after callinglistview.addHeaderView
orlistview.addFooterView
.The reason is that the initial adapter is wrapped in the
HeaderViewListAdapter
.To get your adapter use this code.
Code taken from here
就我而言,这不起作用。在设置适配器之前,我必须将 footerView 设置为 listView。首先,我在 OnCreate 方法中从布局文件初始化了 loadingView:
然后我在同一方法中使用了此解决方法
:
In my case this didn't work. I had to set a footerView to my listView before setting it's adapter. First I initialized my loadingView from a layout file in OnCreate method:
Then I used this workaround in the same method:
Where
您应该将父视图放入您的膨胀视图中。
You should put parent view to your inflated view.