Android学习--小白易错--TextView

来源:互联网 发布:阿里妈妈数据采集 编辑:程序博客网 时间:2024/06/02 21:54

TextView的setText()方法在设值时,要注意,形参的类型。下面是对一个用于显示数值的TextView设值时,没有转成String类型,导致出错。

public final void setText(int resid)

Since: API Level 1

public final void setText(int resid, TextView.BufferType type)

Since: API Level 1

public final void setText(char[] text, int start, int len)

Since: API Level 1

Sets the TextView to display the specified slice of the specified char array. You must promise that you will not change the contents of the array except for right before another call to setText(), since the TextView has no way to know that the text has changed and that it needs to invalidate and re-layout.

public void setText(CharSequence text, TextView.BufferType type)

Since: API Level 1

Sets the text that this TextView is to display (see setText(CharSequence)) and also sets whether it is stored in a styleable/spannable buffer and whether it is editable.

Related XML Attributes
  • android:text
  • android:bufferType

public final void setText(CharSequence text)

Sets the string value of the TextView. TextView does not accept HTML-like formatting, which you can do with text strings in XML resource files. To style your strings, attach android.text.style.* objects to aSpannableString, or see the Available Resource Types documentation for an example of setting formatted text in the XML resource file.

11-05 10:53:43.780: E/AndroidRuntime(21035): FATAL EXCEPTION: main
11-05 10:53:43.780: E/AndroidRuntime(21035): android.content.res.Resources$NotFoundException: String resource ID #0x191b
11-05 10:53:43.780: E/AndroidRuntime(21035): at android.content.res.Resources.getText(Resources.java:252)
11-05 10:53:43.780: E/AndroidRuntime(21035): at android.content.res.MiuiResources.getText(MiuiResources.java:125)
11-05 10:53:43.780: E/AndroidRuntime(21035): at android.widget.TextView.setText(TextView.java:3642)
11-05 10:53:43.780: E/AndroidRuntime(21035): at com.example.neteasedemo.MainActivity$MyAdapter.getView(MainActivity.java:186)

0 0
原创粉丝点击