android.view.ViewGroup$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams


LinearLayout layout = (LinearLayout) mInflater.inflate(
                    R.layout.cell_check_item, null);
            LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                    UPDeviceInfo.getDeviceWidth(), LayoutParams.WRAP_CONTENT);
            rootLayout.addView(convertView, param);

我希望手动设置layout的宽度,结果就报错(虽然很小的几率报错)

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

 

解决方法:换成从原来的view中获取LayoutParams,而不是新建一个LayoutParams

LinearLayout.LayoutParams param = (android.widget.LinearLayout.LayoutParams) checkLayout
                    .getLayoutParams();
            param.width = UPDeviceInfo.getDeviceWidth();

看来,当一个View已经有了LayoutParams,最好不要添加一个新的LayoutParams。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM