Android setText()不适用于充气布局

Android setText()不适用于充气布局,android,android-layout,nullpointerexception,layout-inflater,Android,Android Layout,Nullpointerexception,Layout Inflater,我正在尝试膨胀线性布局,以便在屏幕上构建自定义部分 String txt = "testing"; LinearLayout rowLink = (LinearLayout)getLayoutInflater().inflate(R.layout.additional_link, null); TextView tvCsAddLink = (TextView)findViewById(R.id.tvCsAddLink);

我正在尝试膨胀
线性布局
,以便在屏幕上构建自定义部分

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);
我的
附加链接.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>

在已膨胀的
LinearLayout
行链接上调用
findViewById(R.id.tvCsAddLink)
,现在您正在当前活动中搜索它。

使用此选项从布局中获取文本视图

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

您已经在
线性布局
行链接
上扩展了XML,并且尝试从
活动
的布局中查找
文本视图
(通常是
main.XML
),因此您需要在布局行链接上查找
文本视图
,如下所示:

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

TextView TVCAddLink=(TextView)rowLink.findViewById(R.id.TVCAddLink);9年后,这仍然非常有用!谢谢你,阿尔斯兰!
TextView tvCsAddLink = (TextView)rowLink.findViewById(R.id.tvCsAddLink);