首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ClassCastException:从FrameLayout到RelativeLayout.Params

ClassCastException:从FrameLayout到RelativeLayout.Params
EN

Stack Overflow用户
提问于 2015-07-22 19:08:14
回答 3查看 596关注 0票数 1

我用下面的代码编写了一个在相对布局中设置图像的方法:

代码语言:javascript
复制
private void setHitImageOnShip (Ship ship, View view) {
            ImageView hitImage = new ImageView(getApplicationContext());
            RelativeLayout layout = (RelativeLayout)findViewById(R.id.layoutSetupActivity);
            //RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) layout.getLayoutParams();
            params.addRule(RelativeLayout.CENTER_IN_PARENT, view.getId());
            hitImage.setLayoutParams(params);
            hitImage.setImageResource(R.drawable.image_hit);
            layout.addView(hitImage);
        }

为什么我会通过强制转换得到下面提到的异常?我没想到在任何地方都会有一个FrameLayout:

代码语言:javascript
复制
java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
            at org.nse.battleship.SetupPlayerFieldActivity$MyDragListener.setHitImageOnShip(SetupPlayerFieldActivity.java:319)

确切地说,我不想要FrameLayout,也不知道它从何而来!在我的layout-xml中根本没有FrameLayout,如下所示:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/layoutSetupActivity"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:weightSum="1"
                android:background="@drawable/background_blue"
                android:layout_gravity="top|center_horizontal" >
        <ImageView
                android:id="@+id/imageSetzeSchiffe"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|top"
                android:src="@drawable/image_schiffe_setzen"
                android:contentDescription="@string/startscreen"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true">
        </ImageView>
        <ImageView
                android:id="@+id/imagePlayfield"
                android:layout_width="300dp"
                android:layout_height="280dp"
                android:src="@drawable/image_playfield"
                android:contentDescription="@string/startscreen"
                android:layout_gravity="left|bottom"
                android:layout_below="@+id/imageSetzeSchiffe"
                android:layout_alignParentLeft="true"/>
        <!--<ImageButton
                android:id="@+id/buttonDrehen"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/button_drehen"
                android:contentDescription="@string/startscreen"
                android:layout_gravity="center"
                android:layout_alignBottom="@+id/imagePlayfield"
                android:layout_toRightOf="@+id/imagePlayfield"
                android:layout_marginLeft="24dp"/>-->
       <!-- <ImageButton
                android:id="@+id/button_a1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/button_a1"
                android:contentDescription="@string/startscreen"
                android:layout_gravity="center"
                android:layout_alignBottom="@+id/imagePlayfield"
                android:layout_toRightOf="@+id/imagePlayfield"
                android:layout_marginLeft="-276dp"
                android:layout_toLeftOf="@+id/buttonDrehen"
                android:layout_alignTop="@+id/imageShipBig"/>-->
        <ImageButton
                android:id="@+id/buttonSpielen"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/button_spielen"
                android:contentDescription="@string/startscreen"
                android:layout_gravity="center"
                android:layout_below="@+id/imageShipSpeedboat"
                android:layout_toRightOf="@+id/imageShipCarrier"
                android:layout_toEndOf="@+id/imageShipCarrier"
                android:layout_marginTop="15dp"/>
        <ImageView
                android:id="@+id/imageShipCarrier"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/image_ship_carrier"
                android:contentDescription="@string/startscreen"
                android:layout_gravity="center"
                android:layout_alignTop="@+id/imageShipCruiser"
                android:layout_toRightOf="@+id/imagePlayfield"
                android:layout_marginLeft="40dp"
                android:layout_toEndOf="@+id/imageSetzeSchiffe"/>
        <!--android:layout_toRightOf="@+id/buttonDrehen"/>-->
        <ImageView
                android:id="@+id/imageShipCruiser"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/image_ship_cruiser"
                android:contentDescription="@string/startscreen"
                android:layout_gravity="center"
                android:layout_alignTop="@+id/imagePlayfield"
                android:layout_toLeftOf="@+id/imageShipGunboat"
                android:layout_marginRight="31dp"
                android:layout_marginTop="19dp"/>
        <ImageView
                android:id="@+id/imageShipGunboat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/image_ship_gunboat"
                android:contentDescription="@string/startscreen"
                android:layout_gravity="center"
                android:layout_alignTop="@+id/imageShipCruiser" android:layout_alignRight="@+id/buttonSpielen"
                android:layout_alignEnd="@+id/buttonSpielen"/>
        <ImageView
                android:id="@+id/imageShipSubmarine"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/image_ship_submarine"
                android:contentDescription="@string/startscreen"
                android:layout_gravity="center"
                android:layout_above="@+id/buttonSpielen" android:layout_alignRight="@+id/buttonSpielen"
                android:layout_alignEnd="@+id/buttonSpielen"/>
        <ImageView
                android:id="@+id/imageShipSpeedboat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/image_ship_speedboat"
                android:contentDescription="@string/startscreen"
                android:layout_gravity="center"
                android:layout_below="@+id/imageShipCruiser"
                android:layout_alignLeft="@+id/imageShipCruiser"
                android:layout_alignStart="@+id/imageShipCruiser"
                android:layout_marginTop="10dp"/>
</RelativeLayout>
EN

回答 3

Stack Overflow用户

发布于 2015-07-22 19:13:16

layout.getLayoutParams()返回布局父级的类型。所以你的相对布局可能在框架布局里面,这就是它抛出异常的原因。

在您的情况下,删除

代码语言:javascript
复制
RelativeLayout layout = RelativeLayout)findViewById(R.id.layoutSetupActivity);

并使用

代码语言:javascript
复制
RelativeLayout.LayoutParams params = (RelativeLayout)hitImage.getLayoutParams()
票数 0
EN

Stack Overflow用户

发布于 2015-07-22 19:15:54

您需要设置ImageView所在的ViewGroupLayoutParams。所以你的代码应该像这样

代码语言:javascript
复制
layout.addView(hitImage, params);

我认为您正在尝试将ImageView放在您的RelativeLayout.的中心

票数 0
EN

Stack Overflow用户

发布于 2015-07-22 19:32:53

错误发生在创建/获取这些错误的layoutParams时。

我删除了

代码语言:javascript
复制
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) layout.getLayoutParams();

并使用

代码语言:javascript
复制
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

而不是。看起来,hitImage比其他imageView更正确,而不是像在中间那样。怎么啦?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31561391

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档