Exception: java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
RelativeLayout title_bg = (RelativeLayout)FTU_Bluetooth.this.findViewById(R.id.titlebar);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 0x55);
title_bg.setLayoutParams(params);
如果用RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(int, int),则会报Exception: java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
这是因为在这里的RelativeLayout在xml里是套在LinearLayout里的,必须建立父控件的LayoutParams。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
> <RelativeLayout
android:id="@+id/titlebar"
android:background="@drawable/main_title"
android:layout_width="fill_parent"
android:layout_height="66dp"
>
--------------------------------------------------------------------------
下面转帖壮志凌云的博客: http://sunjilife.blog.51cto.com/3430901/1159639
在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重新设定宽高等布局属性,你为该View设置的布局参数类型与其父类所使用的布局参数类型一样。此外就是说若是最上层的布局,则不需要设定此项。android中提供的布局参数类型目前一共12种:ViewPager.LayoutParams,LinearLayout.LayoutParams,RelativeLayout.LayoutParams,TableLayout.LayoutParams等等。关于
LayoutParams的说明http://www.cnblogs.com/shaweng/archive/2012/07/10/2585134.html
比如:
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <FrameLayout
- android:id="@+id/FrameLayout01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- </LinearLayout>
- 若想在代码中动态改变FrameLayout的大小,应该这样写:
- FrameLayout frameLayout=(FrameLayout) convertView.findViewById(R.id.FrameLayout01);
- LinearLayout.LayoutParams ff=new LinearLayout.LayoutParams(LayoutParams.WRAP_C
- ONTENT, height);
- frameLayout.setLayoutParams(ff);
按照上面的说法,那么若是底层布局是LinearLayout,那么添加view的时候指定的宽高参数就必然是Linear.Layout
Params,可我在尝试过程中发现使用ViewGroup.LayoutParams,RelativeLayout.Params也可以运行,且不会出错,以下是代码:
- //test.xml 布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/test_root_linearlayout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- </LinearLayout>
- package com.example.aboutexpand;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.ViewGroup;
- import android.widget.LinearLayout;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- public class TestActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- RelativeLayout rootLayout;
- super.onCreate(savedInstanceState);
- setContentView(R.layout.test);
- rootLayout = (LinearLayout) findViewById(R.id.test_root_linearlayout);
- LinearLayout.LayoutParams rootLinaerParams = new LinearLayout.LayoutParams(
- 100, 100);
- ViewGroup.LayoutParams rootGroupParams = new LinearLayout.LayoutParams(
- 100, 100);
- RelativeLayout.LayoutParams rootRelativeParams = new RelativeLayout.LayoutParams(
- 100, 100);
- TextView testView = new TextView(this);
- testView.setText("根布局测试动态添加组件并设置其大小");
- testView.setLayoutParams(rootGroupParams);
- rootLayout.addView(testView);
- // rootLayout.addView(testView, viewParams);
- }
经过试验,新增加的TextView的布局参数使用LinearLayout.LayoutParams,RelativeLayout.LayoutParams,ViewGroup.LayoutParams都是可以正确显示的,不相信的朋友,自己也可以试试看。至于这到底是什么原因呢,我目前也不清楚,希望有知道的朋友留言指教一下,谢谢
Exception: java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams的更多相关文章
- java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.w ...
- java.lang.ClassCastException android.widget.RelativeLayout LayoutParams 异常
1.在xml布局文件如下所示: <RelativeLayout android:layout_width="match_parent" android:layout_heig ...
- Caused by: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
最近,在android中用代码动态改变某种布局(组件)的高度时,会遇到如题所示的类转换异常.上网查了一下,如下所示: These supply parameters to the parent of ...
- java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.L(转)
09-09 10:19:59.979: E/AndroidRuntime(2767): FATAL EXCEPTION: main09-09 10:19:59.979: E/AndroidRuntim ...
- java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
最近在学习drawerLayout时,遇到这个bug.如下示: java.lang.ClassCastException: android.widget.RelativeLayout cannot b ...
- java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
Caused by: Java.lang.ClassCastException: Android.widget.TextView cannot be cast to android.widget.Ed ...
- 安卓出现错误: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
Caused by: Java.lang.ClassCastException: Android.widget.TextView cannot be cast to android.widget.Ed ...
- java.lang.ClassCastException: android.widget.ImageButton异常处理
在调程序时总是出现异常关闭的现象,log显示: 03-26 07:58:09.528: E/AndroidRuntime(398): Caused by: java.lang.ClassCastExc ...
- java.lang.ClassCastException:android.widget.Button cannot be cast to android.widget.ImageView
今天遇到一个错误也不知道怎么回事,上网搜了一下: 出现的问题是:java.lang.ClassCastException:android.widget.Button cannot be cast to ...
随机推荐
- zynq修改ramdisk文件系统
⑴ 挂载 Ramdisk新建目录 tmp, 并将 uramdisk.image.gz 拷贝至该目录$ cd <WORKDIR>/Filesystem$ mkdir tmp$ cp uram ...
- 【例题3-3 UVA - 401】Palindromes
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 如果一个字符没有对应的镜像,那么它对应的是一个空格. 然后注意 aba这种情况. 这种情况下b也要查一下它的镜像是不是和b一样. [ ...
- 黑马day18 jquery高级特性&Ajax的load方法
介绍jquery中的load方法: (1).前面没有jquery.修饰,能够判断出他是一个普通的非全局函数(也就是说是一个局部函数):$.,$().,jquery.等修饰的就是全局函数.没有这些修饰的 ...
- 使用RxPermissions(基于RxJava2)
使用RxPermissions(基于RxJava2) 0. 背景 Android 6.0 (API level 23)中.将权限分成了两类.一类是Install权限(称之为安装时权限).还有一类是Ru ...
- 【u118】日志分析
Time Limit: 1 second Memory Limit: 128 MB [问题描述] M 海运公司最近要对旗下仓库的货物进出情况进行统计.目前他们所拥有的唯一记录就是一个记录集装箱进出情况 ...
- PatentTips - Multi-host SATA Controller
BACKGROUND The present subject matter relates, in general, to a computing system having multi-host p ...
- zTree异步加载(自定义图片)
原文链接:https://blog.csdn.net/qq_37936542/article/details/78429675 zTree官网:点击打开链接 一:文件下载 点击首页右下角的ztree ...
- Hdu4771(杭州赛区)
Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- js如何实现动态克隆一个表格?
js如何实现动态克隆一个表格? 一.总结 1.通过innerHTML实现表格内容复制, 2.通过表格dom的属性(比如border)实现属性赋值, 3.通过表格dom的样式style实现样式的复制. ...
- AE属性表操作
转自chanyinhelv原文AE属性表操作 实现的操作包括:1.打开属性表:2.编辑属性表:3.增加属性列:4.数据排序:5.字段计算…… 嗯,实现的功能目前就这些吧,后续还会继续跟进,还望大家多多 ...