HeaderViewListAdapter cannot be cast to listAdapter问题原因及解决办法
2018-03-28 11:07
567 查看
https://www.cnblogs.com/misybing/p/5042482.html
在listView中添加leaderView 和footerView的时候要注意在setAdapter之前调用,不然会报如下异常:
listAdapter cannot be cast to android.widget.HeaderViewListAdapter
此时,listView的表头表尾已作为第一项和最后一项加入listView,可以正常显示及随条目上下滚动。
但在操作该表listItem的时候会报如下错误:
java.lang.ClassCastException: android.widget.HeaderViewListAdapter cannot be cast to [package_name].listAdapter
会发现该错误与上述错误描述刚好相反,其实它们本质上是同一个问题。上一个是在setAdapter之前要set表头表尾,而此时要调用listAdapter,但因为表头表为加入后该adapter被封装成为HeaderViewListAdapter
那么要如何取到原来的Adapter呢?
只需将
改为
由此也可以明确,当增加header或者footer时,lv_data.getAdapter()).getWrappedAdapter().getCount()与 ((HeaderViewListAdapter)listview.getAdapter()).getWrappedAdapter().getCount() 是相差1的,在调用时需要注意。
在listView中添加leaderView 和footerView的时候要注意在setAdapter之前调用,不然会报如下异常:
listAdapter cannot be cast to android.widget.HeaderViewListAdapter
此时,listView的表头表尾已作为第一项和最后一项加入listView,可以正常显示及随条目上下滚动。
但在操作该表listItem的时候会报如下错误:
java.lang.ClassCastException: android.widget.HeaderViewListAdapter cannot be cast to [package_name].listAdapter
会发现该错误与上述错误描述刚好相反,其实它们本质上是同一个问题。上一个是在setAdapter之前要set表头表尾,而此时要调用listAdapter,但因为表头表为加入后该adapter被封装成为HeaderViewListAdapter
那么要如何取到原来的Adapter呢?
只需将
改为
由此也可以明确,当增加header或者footer时,lv_data.getAdapter()).getWrappedAdapter().getCount()与 ((HeaderViewListAdapter)listview.getAdapter()).getWrappedAdapter().getCount() 是相差1的,在调用时需要注意。
相关文章推荐
- HeaderViewListAdapter cannot be cast to listAdapter问题原因及解决办法
- 奇葩问题cannot be cast to android.widget.HeaderViewListAdapter
- 异常 java.lang.ClassCastException:cannot be cast to android.widget.HeaderViewListAdapter.的解决方法
- cannot be cast to android.widget.HeaderViewListAdapter
- Error: cannot be cast to android.widget.HeaderViewListAdapter
- cannot be cast toandroid.widget.HeaderViewListAdapter
- java.lang.ClassCastException: android.widget.HeaderViewListAdapter cannot be cast to com.android.mms
- [android] cannot be cast to android.widget.HeaderViewListAdapter
- cannot be cast to android.widget.HeaderViewListAdapter
- cannot be cast to android.widget.HeaderViewListAdapter
- cannot be cast to android.widget.HeaderViewListAdapter
- cannot be cast to android.widget.HeaderViewListAdapter
- cannot be cast to android.widget.HeaderViewListAdapter
- actionbar出现java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to 的解决办法
- java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.TextView解决办法
- Validator问题及解决办法——java.lang.ClassCastException: org.apache.struts.validator.ValidatorForm cannot be cast to forms.RegisterForm
- java.lang.ClassCastException: com.sun.proxy.$Proxy0 cannot be cast to java.sql.Connection异常问题解决
- 类型转换错误类似java.math.BigDecimal cannot be cast to java.lang.Double 解决办法
- 解决java中的java.lang.Integer cannot be cast to java.lang.Double问题
- Java中解决 cannot be cast to java.io.Serializable的问题