3

J'essaye de gonfler un LinearLayout pour construire une section personnalisée sur mon écran.setText() ne fonctionne pas sur une disposition gonflée

String txt = "testing"; 
LinearLayout rowLink = (LinearLayout)getLayoutInflater().inflate(R.layout.additional_link, null); 
TextView tvCsAddLink = (TextView)findViewById(R.id.tvCsAddLink);          
tvCsAddLink.setText(txt); 

// adding this inflated layout to an existing layout 
myLinearLayout.addView(rowLink); 

Le contenu de mon additional_link.xml se présente comme suit:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/llCsAddLink" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/white_row" 
    android:clickable="true" 
    >   
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:src="@drawable/youtube_icon" />   
    <TextView 
     android:id="@+id/tvCsAddLink"  
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"   
     android:paddingLeft="10px" 
     android:paddingRight="20px" 
     android:textSize="16dp" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:layout_gravity="center_vertical" /> 
</LinearLayout> 

Je reçois NullPointerException sur la ligne:

tvCsAddLink.setText(txt); 
+4

TextView tvCsAjouteLink = (TextView) rowLink.findViewById (R.id.tvCsAddLink); –

Répondre

6

Utilisez ceci pour obtenir TextView à partir de la mise en page.

TextView tvCsAddLink = (TextView)rowLink.findViewById(R.id.tvCsAddLink);          
3

Appel findViewById(R.id.tvCsAddLink) sur votre LinearLayout gonflé rowLink, maintenant vous le recherchez dans l'activité en cours.

4

vous avez gonflé votre XML sur votre linearLayoutrowLink, et vous essayez de trouver la TextView de la mise en page de votre Activity (usualy main.xml), de sorte que vous devez trouver votre textView sur votre mise en page rowLink comme ceci:

TextView tvCsAddLink = (TextView)rowLink.findViewById(R.id.tvCsAddLink); 
Questions connexes