0

Я пишу метод, чтобы установить изображение в пределах относительного расположения с этим кодом: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> 
+0

Почему вы просто не набрасываете 'params' на FrameLayout.LayoutParams? – Sweeper

+0

Я не ожидаю, что в рамке я использую RelativeLayout. – Pille

+0

RelativeLayout layout = (RelativeLayout) findViewById (R.id.layoutSetupActivity); изменить идентификатор в xml и проверить ... –

ответ

0

layout.getLayoutParams() возвращает тип макета родителя. Таким образом, ваш относительный макет, вероятно, находится внутри фрейма, и именно поэтому он выдает исключение.

В вашем случае удалить

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

и использовать

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

Это не тот случай. В моем макете нет FrameLayout. – Pille

+0

Я думаю, что основной контейнер для Android - Framelayout. В любом случае, если вы используете hitImage.getLayoutParams() будет работать – vandzi

+0

Итак, вы рекомендуете получать параметры макета из hitImage? – Pille

0

Вы должны установить LayoutParams в ViewGroupImageView сидит. Так что ваш код должен подобные

layout.addView(hitImage, params); 

Я думаю, вы пытаетесь поставить ImageView в центр вашего RelativeLayout.

+0

Я хотел бы установить hitView в центре представления, заданного в качестве параметра метода. – Pille

+0

Мой код snipet делает именно эту вещь. Здесь hitImage будет добавлен в центр макета. –

+0

Но код пока не дошел до layout.addView (....). Он остановился раньше в строке: RelativeLayout.LayoutParams params = (RelativeLayout) hitImage.getLayoutParams(); – Pille

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, а не так, как предполагалось в центре. Что не так?

 Смежные вопросы

  • Нет связанных вопросов^_^