Я пишу метод, чтобы установить изображение в пределах относительного расположения с этим кодом:ClassCastException: FrameLayout к RelativeLayout.Params
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 и понятия не имею, откуда приходит! В моей верстки-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>
Почему вы просто не набрасываете 'params' на FrameLayout.LayoutParams? – Sweeper
Я не ожидаю, что в рамке я использую RelativeLayout. – Pille
RelativeLayout layout = (RelativeLayout) findViewById (R.id.layoutSetupActivity); изменить идентификатор в xml и проверить ... –