ClassCastException:FrameLayout到RelativeLayout.Params

时间:2015-07-22 11:08:14

标签: android android-layout imageview relativelayout layoutparams

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

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:

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,见下文:

<?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>

3 个答案:

答案 0 :(得分:0)

layout.getLayoutParams()返回布局父类型。因此,您的相对布局可能在框架布局内,这就是它抛出异常的原因。

在您的情况下,请删除

RelativeLayout layout = RelativeLayout)findViewById(R.id.layoutSetupActivity);

并使用

RelativeLayout.LayoutParams params = (RelativeLayout)hitImage.getLayoutParams()

答案 1 :(得分:0)

您需要设置 ViewView ImageView LayoutParams 。所以您的代码应该是

layout.addView(hitImage, params);

我认为您试图将 ImageView 置于 RelativeLayout 的中心。

答案 2 :(得分:0)

错误在于创建/获取那些layoutParams错误。

我删除了

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) layout.getLayoutParams();

并使用

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

代替。 因为看起来hitImage是正确的另一个imageView而不是中心的意图。有什么问题?