5月14日更新
如果我更换它,它是打破它的文本大小的混合
- 16sp
只是改变了颜色
- #00ff00
然后它运行正常.
对LogLine的引用,在logcat中的测量应该给了我一个线索.我仍然想修复它,我相信其他人会要求在单个文本行中混合使用大小.
Udpdated 5月12日 - 本文末尾显示了最小代码示例
在SDK 2.everything下工作正常的代码,在3.0模拟器中运行时抛出空指针异常.我已经将它缩小到我的代码中唯一出现的SpannableString.我正在使用它将一些不同字体大小的文本放在屏幕顶部的横幅区域中.logcat是:
FATAL EXCEPTION: main java.lang.NullPointerException at android.text.style.TextAppearanceSpan.updateDrawState(TextAppearanceSpan.java:209) at android.text.TextLine.handleRun(TextLine.java:848) at android.text.TextLine.measureRun(TextLine.java:399) at android.text.TextLine.measure(TextLine.java:278) at android.text.TextLine.metrics(TextLine.java:252) at android.text.Layout.measurePara(Layout.java:1432) at android.text.Layout.getDesiredWidth(Layout.java:89) at android.text.Layout.getDesiredWidth(Layout.java:68) at android.widget.TextView.onMeasure(TextView.java:5713) at android.view.View.measure(View.java:10577) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365) at android.view.View.measure(View.java:10577) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365) at android.view.View.measure(View.java:10577) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:581) at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:365) at android.view.View.measure(View.java:10577) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4270) at android.widget.FrameLayout.onMeasure(FrameLayout.java:267) at android.view.View.measure(View.java:10577) at android.widget.LinearLayout.measureVertical(LinearLayout.java:764) at android.widget.LinearLayout.onMeasure(LinearLayout.java:519) at android.view.View.measure(View.java:10577) at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4270) at android.widget.FrameLayout.onMeasure(FrameLayout.java:267) at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:1876) at android.view.View.measure(View.java:10577) at android.view.ViewRoot.performTraversals(ViewRoot.java:885) at android.view.ViewRoot.handleMessage(ViewRoot.java:1944) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:126) at android.app.ActivityThread.main(ActivityThread.java:3997) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:491) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) at dalvik.system.NativeStart.main(Native Method)
代码位于由计时器调用的处理程序中,它使用以下基本行写入文本视图(省略空检查等)
TextView tvBanner = (TextView) findViewById(R.id.RefText); ..... int startSpan = altDispStr.indexOf("\n"); int endSpan = altDispStr.length(); spanRange = new SpannableString(altDispStr); spanRange.setSpan(new TextAppearanceSpan(this, R.style.custompoint), startSpan, endSpan, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); .... .... tvBanner.setText(spanRange);
当我将它捕获到调试器中时(使用3.0模拟器和3GB内存不容易)它通过处理程序方法OK, - spanRange不为null,但NPE和FC稍后会在主循环器中发生.
textview(RefTxt)位于一个名为infobar.xml的xml中,该文件包含在main.xml中.信息栏的基本组成部分是:
如果我用普通的旧String替换SpannableString,那么应用程序将在3.0模拟器中运行.我想知道我是否需要使用Spannables和平板电脑设备采取额外的措施?
当它死亡时,调试透视图显示 - mt和tl都不为空
更新示例 我已将此缩减为演示错误的最小代码示例,在2.1下可以正常但在3.0下崩溃相同的NPE
活动代码:
public class SpanTest extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String dispStr = "I'm the first line\nI'm the second line"; TextView tvBanner = (TextView) findViewById(R.id.textView1); int startSpan = dispStr.indexOf("\n"); int endSpan = dispStr.length(); Spannable spanRange = new SpannableString(dispStr); spanRange.setSpan(new TextAppearanceSpan(this, R.style.custompoint), startSpan, endSpan, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); tvBanner.setText(spanRange); } }
main.xml是一个只包含一个TextView的LinearLayout.我的包含custompoint的styles.xml是:
.
您的TextView正在使用没有该linkColor
集的TextPaint对象.我看到你使用的样式/主题不知何故没有设置linkColor.尝试在样式中添加linkColor,看看是否能解决您的问题.
编辑:您应该继承默认样式,这将为您提供所需的内容.不确定为什么行为从2.1变为3.0,但......