android.widget.FrameLayout cannot be cast to android.widget.Button 问题
来源:互联网 发布:牛奶推荐 知乎 编辑:程序博客网 时间:2024/06/06 07:33
今天在写Fragment 布局的时候遇到个问题。
代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="4dip" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center_horizontal"
android:measureWithLargestChild="true"
android:orientation="horizontal"
android:padding="4dip" >
<Button
android:id="@+id/bt_push"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Push" >
<requestFocus />
</Button>
<Button
android:id="@+id/bt_pop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Pop" >
</Button>
</LinearLayout>
<FrameLayout
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="帅气系列"
android:textAppearance="?android:attr/textAppearanceLarge" />
</FrameLayout>
</LinearLayout>
报错信息为:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jerryl.fragmentstackdemo/com.jerryl.fragmentstackdemo.MainActivity}: java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.widget.Button
仔细考虑下,可能是因为 FrameLayout 的 android:layout_weight="1" ,权重的问题。
修改成为下面这个样子后,程序就没事了。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.jerryl.fragmentstackdemo.MainActivity" >
<FrameLayout
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="帅气系列" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:id="@+id/bt_push"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="push" />
<Button
android:id="@+id/bt_pop"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="pop" />
</LinearLayout>
</RelativeLayout>
不用 LinearLayout ,改用 RelativeLayout,而 RelativeLayout 中没有权重的概念。
LinearLayout 中权重的使用有许多需要注意的地方,可以参考下面这篇博客文章:
http://blog.csdn.net/yanzi1225627/article/details/24667299
- android.widget.FrameLayout cannot be cast to android.widget.Button 问题
- android.widget.Button cannot be cast to android.widget.Gallery
- widget.Button cannot be cast to android.widget.ListView
- 安卓出现android.widget.TextView cannot be cast to android.widget.Button问题
- android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
- android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
- android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
- android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
- 【Android】程序崩溃错误为Android.widget,textView cannot be cast to android.widget.button
- 【Android】程序崩溃错误为Android.widget,textView cannot be cast to android.widget.button
- 关于Android.widget.Button cannot be cast to com.esri.android.map.MapView错误的问题
- 错误android.widget.ImageButton cannot be cast to android.widget.Button
- java.lang.ClassCastException~android.widget.ImageView cannot be cast to android.widget.Button~
- 安卓异常android.widget.TextView cannot be cast to android.widget.Button
- APP程序崩溃错误为Android.widget,textView cannot be cast to android.widget.button
- cannot be cast to android.widget.HeaderViewListAdapter
- cannot be cast to android.widget.HeaderViewListAdapter
- cannot be cast to android.widget.HeaderViewListAdapter
- 120. Triangle
- 操作系统服务:其它模块
- BFPTR算法
- C++ Socket 学习笔记
- java--javaWeb--终极版JDBCUtils,使用事务、commons-dbutils、c3p0
- android.widget.FrameLayout cannot be cast to android.widget.Button 问题
- Mac下Apktool的配置与应用
- 虚函数与多态
- Median of Two Sorted Arrays(hard)
- HTML5中input背景提示文字(placeholder)的CSS美化
- 链表常见面试题
- JAVAweb开发技术-------(三)JSP (封装自己的标签库)
- SharedPreferences存储图片对象与获取
- 从git仓库中下载项目到Studio中