Ajout d'image par programmation à RelativeLayout

Je veux ajouter à un linearlayout, diverses dispositions relatives via le code. Chaque parent mises en page se compose de: une imageview à gauche, un textview côté de la droite (exactement au milieu) et une autre Image à droite. Je dois les ajouter à l'aide des données lues à partir d'une base de données. Il doit être relativelayout parce que je veux utiliser un écouteur sur l'image et un autre auditeur au cours de la RL.

Chaque relativelayout ressemble à cette chose que j'ai en XML.

<RelativeLayout
    android:id="@+id/relativecanal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

       <ImageView
           android:id="@+id/imageView1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentLeft="true"
           android:layout_alignParentTop="true"
           android:src="@drawable/antena3" />

       <TextView
           android:id="@+id/textView1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_centerVertical="true"
           android:layout_marginLeft="39dp"
           android:layout_toRightOf="@+id/imageView1"
           android:text="Large Text"
           android:textAppearance="?android:attr/textAppearanceLarge" />

       <ImageView
           android:id="@+id/imageView2"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentRight="true"
           android:layout_centerVertical="true"
           android:src="@drawable/star" />

    </RelativeLayout>

Je suis en utilisant ce code: mais j'obtiens une exception NullPointerException dans les moments de la prise de la addView() pour l'image, j'ai essayé tout à l'ajout de l'textView et il fonctionne.

    try {
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,archivoCanales);
LinearLayout layout = (LinearLayout) findViewById(R.id.channelslistlayout);
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
RelativeLayout channel=new RelativeLayout(this);
channel.setBackgroundColor(2);
TextView channelName=new TextView(this);
channelName.setText(new String(line));
ImageView image=(ImageView)findViewById(R.drawable.animalplanet);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);    
RelativeLayout.LayoutParams firstImageParams = new RelativeLayout.LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
firstImageParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
RelativeLayout.LayoutParams secImageParams = new RelativeLayout.LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
secImageParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
channel.addView(image,firstImageParams);
channel.addView(channelName,secImageParams);
layout.addView(channel,llp);
}
}
catch (IOException e) {
e.printStackTrace();
}

OriginalL'auteur danywarner | 2012-05-03