API11(以及now12)和Spannable导致运行时NPE [英] API11 (and now12) and Spannable causing runtime NPE

查看:132
本文介绍了API11(以及now12)和Spannable导致运行时NPE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新5月14日

这是文本大小的组合,打破它,如果我更换

It's the mix of text sizes that breaks it, if I replace the

<item name  = "android:textSize">16sp</item> 

与颜色的只是改变像

<item name="android:textColor">#00ff00</item>

那么它运行正常。

then it runs OK.

要一个TextLine,措施在logcat中的参考文献应该给我一个线索。 我还是喜欢,虽然解决这个问题,我相信其他人都会有要求的尺寸在一个单一的文本行的组合。

The references to TextLine,measure in the logcat should have given me a clue. I'd still like to fix it though, I'm sure others will have a requirement for a mix of sizes in a single text line.

Udpdated 5月12日 - 最小的code例子显示在文章最后

Udpdated 12 May - minimal code example is shown at the end of this post

code这下SDK 2.everything正常工作,抛出一个空指针异常。我已经收窄,到我的code中的唯一一个出现的SpannableString。我使用此来把不同字体大小的一些文字在标题区域在屏幕的顶部。该logcat的是:

Code which works fine under SDK 2.everything, throws a null pointer exception when run in a 3.0 emulator. I've narrowed it down to the only occurrence of a SpannableString in my code. I'm using this to put some text of differing font sizes in a banner area at the top of the screen. The logcat is:

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)

在code是在一个名为由定时器处理程序,将其写入具有以下基本行文本视图(null检查等略)

The code is in a handler called by a timer, it writes to a text view with the following essential lines (null checks etc omitted)

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);

当我在调试器中它获得通过的处理方法OK,(与3.0仿真器和3GB的内存不容易的)陷阱它 - spanRange不为空,但NPE和FC晚些时候发生在主尺蠖

When I trap it in the debugger (not easy with the 3.0 emulator and 3GB of RAM) it gets through the handler method OK, - spanRange is not null, but the NPE and FC happens sometime later in the main looper.

TextView的(RefTxt)是在其中包括在main.xml中一个xml称为infobar.xml。信息栏的基本组成部分是:

The textview (RefTxt) is in an xml called infobar.xml which is included in the main.xml. The infobar's essential components are:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/BannerRelView"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#800000ff"
android:layout_width="wrap_content">
    <TextView
       android:id="@+id/BannerOptionsButton"      
       android:layout_alignParentRight="true"   
       ....     
     </TextView>
     <TextView
         android:id="@+id/BannerGPS"
         android:layout_toLeftOf="@+id/BannerOptionsButton"    
         ....
      </TextView>
      <TextView
         android:id="@+id/RefText"
         android:layout_toLeftOf="@+id/BannerGPS"
         ......
      </TextView>
</RelativeLayout>

如果我更换SpannableString一个普通的旧字符串然后应用程序运行在3.0的模拟器。 我不知道是否有任何额外的措施,我需要与Spannables和平板设备?

If I replace the SpannableString with a plain old String then the app runs in 3.0 emulators. I'm wondering if there are any extra measures I need to take with Spannables and tablet devices?

当它死,调试透视图显示 - MT和TL都是不为空

When it dies, the debug perspective shows - mt and tl are both not null

更新例如 我已经减少这这表明误差最小的code为例,它的确定2.1下,但具有相同NPE在3.0崩溃

Update example I've reduced this to the smallest code example which demonstrates the error, it's OK under 2.1 but crashes with the same NPE under 3.0

活动code:

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中是LinearLayout中containg只是一个TextView的。我含custompoint styles.xml是:

main.xml is a LinearLayout containg just the one TextView. My styles.xml containing custompoint is:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style
        name="custompoint">
        <item name="android:textSize">24sp</item>
        <item name="android:textStyle">bold</item>
    </style>
</resources>

推荐答案

这不具有 LINKCOLOR 设置。我看你正在使用的样式/主题有点不设置LINKCOLOR。尝试添加LINKCOLOR的风格,看看是否能解决您的问题。

Your TextView is using a TextPaint object that doesn't have the linkColor set. I see the style/theme you are using is somehow not setting the linkColor. Try adding a linkColor to the style and see if that solves your problem.

编辑:你应该继承的默认样式,并且会给你想要的东西。不知道为什么的行为从2.1改为3.0,但...

you should probably inherit the default style, and that will give you what you want. Not sure why the behavior changed from 2.1 to 3.0, but...

这篇关于API11(以及now12)和Spannable导致运行时NPE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆