android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

慕嘉运
2023-12-01

java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

这个是在为BaseAdapter设置 子视图的时候出现的exception,log显示出运行时错误,如上.


当在控件中添加子控件的时候,需要添加布局信息.就会存在如下情况:

TextView tv1 = new TextView(MainActivity.this);

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);

tv1.setLayoutParams(lp);

如果tv1的父控件是 RelativeLayout,则无误.

如果tv1的父控件的AbsListView,则会出现上述运行时错误.


看看关于LayoutParams的官方解释:(自sdk)

LayoutParams are used by views to tell their parents how they want to be laid out. See ViewGroup Layout Attributesfor a list of all child view attributes that this class supports.

The base LayoutParams class just describes how big the view wants to be for both width and height. For each dimension, it can specify one of...

即:子空间用LayoutParams 来告诉父控件 如何布局.

故:AbsListView为子控件分配空间的时候,获取的是 AbsListView.LayoutParams,而不是

RelativeLayout.LayoutParams.


 类似资料:

相关阅读

相关文章

相关问答