2014-05-19 3 views
0

J'ai quelques problèmes avec ce code. Je suis en train de créer une liste dynamique des nouvelles avec ce format:ClassCastException: android.widget.RelativeLayout ne peut pas être casté en android.widget.LinearLayout


|image| - title 
|image|- subtitle 

c'est mon petit code cicle (un exemple avec des données aléatoires)

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); 

et ceci est ma disposition:

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

lorsque je lance l'activité avec ce code que j'ai ce problème:

05-19 09:12:45.780: E/AndroidRuntime(21729): java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.LinearLayout

Je suis en train de créer une mise en page avec seulement le code Java ... mais rien XD où je me trompe?

Répondre

6

changement

LinearLayout lin = (LinearLayout)findViewById(R.id.linear_sezione_news); 

à

RelativeLayout lin = (RelativeLayout)findViewById(R.id.linear_sezione_news); 

L'id linear_sezione_news est affecté à un RelativeLayout de sorte que vous ne pouvez pas le jeter aux LinearLayout

+0

... ce problème stupide! XD la prochaine fois que je lis mieux mon code;) très – Odino

+0

merci maintenant cette erreur: java.lang.NullPointerException à la ligne: titolo.setText ("titolooooo"); – Odino

Questions connexes