在Oreo中设置长文本时,TextView.setText会给出IndexOutOfBoundException

时间:2018-05-06 18:17:46

标签: android

我使用setText:

从String资源文件中设置一个长字符串
W/ResourceType: Bad string block: string #968 is not null-terminated
                CREATING STRING CACHE OF 43224 bytes
                Bad string block: string #968 decoded length is not correct -1 vs 23209
W/System.err: java.lang.IndexOutOfBoundsException
                  at android.content.res.StringBlock.nativeGetString(Native Method)
                  at android.content.res.StringBlock.get(StringBlock.java:82)
                  at android.content.res.AssetManager.getResourceValue(AssetManager.java:236)
W/System.err:     at android.content.res.AssetManager.getResourceText(AssetManager.java:164)
                  at android.content.res.Resources.getText(Resources.java:334)
                  at android.widget.TextView.setText(TextView.java:5494)
                  at com.test.test.activities.QTestActivity.onCreate(TestDetailsActivity.java:92)
                  at android.app.Activity.performCreate(Activity.java:6975)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2784)
W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2906)
                  at android.app.ActivityThread.-wrap11(Unknown Source:0)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1605)
                  at android.os.Handler.dispatchMessage(Handler.java:105)
                  at android.os.Looper.loop(Looper.java:172)
                  at android.app.ActivityThread.main(ActivityThread.java:6637)
                  at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

这是xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        android:paddingBottom="10dp"
        android:paddingLeft="4dp"
        android:paddingRight="4dp"
        android:scrollbars="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:id="@+id/v_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/iv_background"
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:clickable="false"
                android:contentDescription="@null"
                android:scaleType="centerInside"
                android:src="@drawable/header_bg"/>

            <TextView
                android:id="@+id/tv_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clickable="false"
                android:gravity="center"
                android:textColor="@android:color/black"
                android:textSize="24sp"
                tools:ignore="RtlHardcoded"/>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>


</android.support.design.widget.CoordinatorLayout>

在java文件中,我所做的是:

tv_text.setText(R.string.long_text);

之前我没有遇到任何问题,我将Android工作室和Google Play服务更新为最新版本: 15.0.0 ,现在我面临这个问题。

对此有何解决方案?

2 个答案:

答案 0 :(得分:1)

尝试使用

  

mTextSample.setText(Html.fromHtml(文本));

示例:

String text = "This is <font color='red'>simple</font>.";
textView.setText(Html.fromHtml(text), TextView.BufferType.SPANNABLE);

答案 1 :(得分:0)

 textView.setText(Html.fromHtml(getResources().getString(R.string.app_text), Html.FROM_HTML_MODE_LEGACY));