3

LinearLayout을 부풀려서 화면에 사용자 정의 섹션을 작성하려고합니다.비정상 배치에서 setText()가 작동하지 않습니다.

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

은 다음 내 additional_link.xml의 내용대로 :

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

내가 라인에 NullPointerException 받고 있어요 :

tvCsAddLink.setText(txt); 
+4

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

답변

6

레이아웃에서 TextView를 가져 오는 데 사용합니다.

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

전화 당신의 팽창 LinearLayoutrowLinkfindViewById(R.id.tvCsAddLink), 이제 현재 활동에서 검색하고 있습니다.

4

당신은 당신의 linearLayoutrowLink에 XML을 팽창 한, 당신은 당신의 당신이 당신의 레이아웃 textView이 같은 rowLink하여 찾을 필요가 Activity (데는 보통 main.xml), 그래서의 레이아웃에서 TextView 찾아보십시오 :

TextView tvCsAddLink = (TextView)rowLink.findViewById(R.id.tvCsAddLink); 
관련 문제