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

0 0