Android setText error
题
I have a view in the XML
<TextView style="@style/TitleBarText" />
whose styles are defined in values/styles.xml as follows
<style name="TitleBarText">
<item name="android:id">@+id/title_text</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:layout_weight">1</item>
<item name="android:gravity">center_vertical</item>
<item name="android:textSize">@dimen/text_size_medium</item>
<item name="android:paddingLeft">12dip</item>
<item name="android:paddingRight">12dip</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">@color/title_text</item>
<item name="android:singleLine">true</item>
<item name="android:ellipsize">end</item>
</style>
When i try to set the text from onCreate of the activity like this i am getting an error
TextView titleText = (TextView) findViewById(R.id.title_text);
titleText.setText(getString(R.string.title_home));
In Eclipse i am getting this error
title_text cannot be resolved or is not a field
解决方案
You have set id of the String resource in textview You have to write like
titleText.setText(getString(R.string.title_home));
EDIT
You have to set id of textview in <Textview>
tag instead of writing in style of textview
不隶属于 StackOverflow