setText() 不适用于膨胀布局
我正在尝试充气 LinearLayout
以在屏幕上构建自定义部分。
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);
I'm trying to inflate a LinearLayout
to build a customized section on my screen.
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);
The content of my additional_link.xml
is as follows:
<?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>
I'm getting a NullPointerException
on the line:
tvCsAddLink.setText(txt);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用它从布局中获取 TextView。
Use this to get TextView from layout.
您已在
linearLayout
rowLink
上夸大了 XML,并且尝试从Activity
的布局中查找TextView
> (通常是main.xml
),因此您需要在布局 rowLink 上找到textView
,如下所示:you have inflated your XML on your
linearLayout
rowLink
, and you try to find theTextView
from the layout of yourActivity
( usualymain.xml
) , so you need to find yourtextView
on your layout rowLink like this :在膨胀的
LinearLayout
rowLink 上调用findViewById(R.id.tvCsAddLink)
,现在您正在当前活动中搜索它。Call
findViewById(R.id.tvCsAddLink)
on your inflatedLinearLayout
rowLink, now you are searching it in the current activity.