开发问题及解决 java.lang.ClassCastException:android.widget.LinearLayout$LayoutParams

Caused by: java.lang.ClassCastException:android.widget.LinearLayout$LayoutParams  

 
  最近,在android中用代码动态改变某种布局(组件)的高度时,会遇到如题所示的类转换异常。上网查了一下,如下所示:

These supply parameters to the parent of this view specifying how it should be arranged. There are many subclasses of ViewGroup.LayoutParams, and these correspond to the different subclasses of ViewGroup that are responsible for arranging their children.

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.

  我是这样理解的,如果你要将一个view添加到另一个布局中,你必须设定该View的布局参数为其父类所使用的布局参数类型。即要在代码中动态改变某组件的高度,其布局参数类型应该是其父类所使用的布局参数类型。
[java]  view plain copy
  1.  比如:  
  2.   
  3.   
  4. <LinearLayout  
  5.   
  6.   xmlns:android="http://schemas.android.com/apk/res/android"    
  7.   
  8.   android:layout_width="wrap_content"  
  9.   
  10.   android:layout_height="wrap_content">    
  11.   
  12.     
  13.   
  14. <FrameLayout   
  15.   
  16. android:id="@+id/FrameLayout01"   
  17.   
  18. android:layout_width="wrap_content"   
  19.   
  20. android:layout_height="wrap_content" />   
  21.   
  22. </LinearLayout>  
  23.   
  24. 若想在代码中动态改变FrameLayout的大小,应该这样写:  
  25.   
  26.   
  27. FrameLayout frameLayout=(FrameLayout) convertView.findViewById(R.id.FrameLayout01);    
  28.   
  29.  LinearLayout.LayoutParams ff=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, height);   
frameLayout.setLayoutParams(ff);
注:其布局参数类型应该线性布局 LinearLayout.LayoutParams 的类型,而不是帧布局 FrameLayout  .LayoutParams。
天梯(tianti) Java 轻量级的 CMS 解决方案-天梯。天梯是一个用 Java 相关技术搭建的后台 CMS 解决方案,用户可以结合自身业务进行相应扩展,同时提供了针对 dao、service 等的代码生成工具。技术选型:Spring Data JPA、Hibernate、Shiro、 Spring MVC、Layer、MySQL 等。 简介: 1、天梯是一款使用Java编写的免费的轻量级CMS系统,目前提供了从后台管理到前端展现的整体解决方案。 2、用户可以不编写一句代码,就制作出一个默认风格的CMS站点。 3、前端页面自适应,支持PC和H5端,采用前后端分离的机制实现。后端支持天梯蓝和天梯红换肤功能。 4、项目技术分层明显,用户可以根据自己的业务模块进行相应地扩展,很方便二次开发。 核心框架:Spring Framework 4.2.5.RELEASE 安全框架:Apache Shiro 1.3.2 视图框架:Spring MVC 4.2.5.RELEASE 数据库连接池:Tomcat JDBC 缓存框架:Ehcache ORM框架:Spring Data JPA、hibernate 4.3.5.Final 日志管理:SLF4J 1.7.21、Log4j 编辑器:ueditor 工具类:Apache Commons、Jackson 2.8.5、POI 3.15 view层:JSP 数据库:mysql、oracle等关系型数据库 前端 dom : Jquery 分页 : jquery.pagination UI管理 : common UI集成 : uiExtend 滚动条 : jquery.nicescroll.min.js 图表 : highcharts 3D图表 :highcharts-more 轮播图 : jquery-swipe 表单提交 :jquery.form 文件上传 :jquery.uploadify 表单验证 :jquery.validator 展现树 :jquery.ztree html模版引擎 :template
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值