您的位置:首页 > 移动开发 > Android开发

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

2013-04-25 15:32 441 查看
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<RelativeLayout
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent">


以上是布局文件代码,我想在代码中重新设置rl的宽度

RelativeLayout relativeLayout = (RelativeLayout)this.findViewById(R.id.rl);
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(100, LayoutParams.MATCH_PARENT);
relativeLayout.setLayoutParams(param);


就会报这个错 Caused by: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams 。

解决方法:relativeLayout 是LinearLayout的子控件,它的LayoutParams 应该是LinearLayout给他的。所以应该是LinearLayout.LayoutParam 。如果relativeLayout 有子控件的话,那它的子控件用的的就是relativeLayout 给他们的,才是RelativeLayout.LayoutParam。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐