ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.LinearLayout
android, android-layout, java
Solution
Change
LinearLayout lin = (LinearLayout)findViewById(R.id.linear_sezione_news);
to
RelativeLayout lin = (RelativeLayout)findViewById(R.id.linear_sezione_news);
The id `linear_sezione_news` is assigned to a `RelativeLayout` so you can't cast it to `LinearLayout`
Problem
I have some problems with this code. I'm trying to create a dynamically list of news with this format: ``` |image| - title |image|- subtitle ``` this is my little cicle code (a example with random data) ``` void setListNews(List<Map<String,String>>l){ listaNewsPagina = l; final Iterator ite = listaNewsPagina.iterator(); LinearLayout lin = (LinearLayout)findViewById(R.id.linear_sezione_news); LinearLayout lineare = (LinearLayout)findViewById(R.id.lineare); while(ite.hasNext()){ Map<String,String> map = (Map<String, String>) ite.next(); ImageView imm = (ImageView)findViewById(R.id.immagine); RelativeLayout rl = (RelativeLayout)findViewById(R.id.relative); TextView titolo = (TextView)findViewById(R.id.titolo); TextView sottoTitolo = (TextView)findViewById(R.id.sottoTitolo); titolo.setText("titolooooo"); sottoTitolo.setText("sottoTitoloooooooooooo"); rl.addView(titolo); rl.addView(sottoTitolo); lineare.addView(imm); lineare.addView(rl); } lin.addView(lineare); setContentView(lin); ``` and this is my layout: ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/linear_sezione_news" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background_homepage" > <LinearLayout android:id="@+id/lineare" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#aadd99" > <ImageView android:id="@+id/immagine" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginBottom="4dp" android:layout_marginLeft="4dp" android:layout_marginRight="10dp" android:layout_marginTop="4dp" android:src="@drawable/ic_launcher" /> <RelativeLayout android:id="@+id/relative" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="4dp" android:layout_marginLeft="4dp" android:layout_marginRight="10dp" android:layout_marginTop="4dp" android:background="#abfc99"> <TextView android:id="@+id/titolo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" /> <TextView android:id="@+id/sottoTitolo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/titolo" android:textSize="13dp" /> </RelativeLayout> </LinearLayout> <ListView android:id="@+id/listViewNews" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@+id/ImageButtonPrecc" android:layout_alignParentLeft="true" android:layout_below="@+id/lineare" android:alpha="0.8" android:background="#aadd99" android:clickable="true" android:padding="10dp" android:textAlignment="center" > </ListView> <ImageButton android:id="@+id/ImageButtonPrecc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_alignParentLeft="true" android:src="@drawable/bottone_prev" android:text="@string/load_news" /> <ImageButton android:id="@+id/ImageButtonSucc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:src="@drawable/bottone_next" android:text="@string/load_news" /> </RelativeLayout> ``` when I launch the activity with this code I have this problem: 05-19 09:12:45.780: E/AndroidRuntime(21729): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.LinearLayout I'm trying to create a layout with only Java code...but nothing XD where I'm wrong?