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 ...
随机推荐
- 6、修改应用程序数码相框以支持自动关闭LCD
1. 修改数码相框以自动关闭LCD关闭LCD : 在读取触摸屏的函数中判断:如果15S内无数据,执行: echo auto > /sys/devices/platform/mylcd/power ...
- 转 OC温故:类的三大特性(封装,继承,多态)
原文标题:OC学习篇之---类的三大特性(封装,继承,多态) 我们都知道,面向对象程序设计中的类有三大特性:继承,封装,多态,这个也是介绍类的时候,必须提到的话题,那么今天就来看一下OC中类的三大特性 ...
- Geodatabase模型
原文 Geodatabase模型 地理数据模型是地理实体及其关系的形式化抽象和数学描述.随着数据库.面向对象等技术的发展,面向对象的地理数据模型成为大型空间数据库的首选方案,它克服了传统地理数据模型的 ...
- Nginx和Nginx+的比較(下)
Nginx和Nginx+的比較(下) 作者:chszs.未经博主同意不得转载.经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs 内容紧接上一篇<Nginx和 ...
- 超链接a的download属性 实现文件下载功能
今天做项目遇到一个要点击按钮下载文件的功能. 百度之 知道了a的download属性.这是HTML5的新特性.主要功能是实现下载功能.主要语法是 <a href="url" ...
- WM_CAP_DRIVER_CONNECT
WM_CAP_DRIVER_CONNECT //ActiveX ---->OnCreate m_pit.Create(IDD_CAM_DIALOG,this); CRect rc; this ...
- [TypeStyle] Reusable styles using TypeStyle mixins
TypeStyle’s style function allows you to give multiple objects as an argument. This provides a simpl ...
- 《Java设计模式》之抽象工厂模式
场景问题 举个生活中常见的样例--组装电脑.我们在组装电脑的时候.通常须要选择一系列的配件,比方CPU.硬盘.内存.主板.电源.机箱等. 为讨论使用简单点.仅仅考虑选择CPU和主板的问题. 其实,在选 ...
- Yarn架构基本概况(一)
1)引言 针对MRv1在扩展性.可靠性,资源利用率和多框架的支持上存在着明显的不足.进而诞生了下一代的MapReduce的计算框架MapReduce Version2,MRV1中有一个非常大的问题就是 ...
- 排查一般MySQL性能问题
排查一般MySQL性能问题,通常要记录下面几项信息: 1.mysql> show processlist; 2.mysql> show engine innodb status\G 3.表 ...