1

尝试将带有文本视图的片段附加到运行 Android 8.1 的像素上的活动中时,出现以下崩溃。我已经在运行 Android 9.0 的 Pixel 2、运行 Android 7.0 的 S6 和运行 Android 6.0 的 S5 上对此进行了测试,并且没有发生崩溃。

碰撞:

    android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class TextView
Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class TextView
Caused by: 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.getPooledStringForCookie(AssetManager.java:332)
    at android.content.res.TypedArray.loadStringValueAt(TypedArray.java:1272)
    at android.content.res.TypedArray.getText(TypedArray.java:175)
    at android.widget.TextView.<init>(TextView.java:1144)
    at android.widget.TextView.<init>(TextView.java:820)
    at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:87)
    at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:83)
    at android.support.v7.app.AppCompatViewInflater.createTextView(AppCompatViewInflater.java:177)
    at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:102)
    at android.support.v7.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
    at android.support.v7.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
    at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:189)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:772)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at com.example.view.fragments.ExcerptFragment.onCreateView(ExcerptFragment.kt:54)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:2435)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
    at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
    at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
    at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
    at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
    at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

片段 XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".view.fragments.ExcerptFragment"
    android:background="@color/background">
    <TextView
        android:id="@+id/excerptText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingStart="5dp"
        android:paddingEnd="5dp"
        android:textColor="@color/textColor"
        android:text="@string/excerpt" />
</ScrollView>

片段类:

class ExcerptFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_excerpt, container, false)
    }
}

摇篮依赖:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0-beta01'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.android.support:support-v4:28.0.0-beta01'
    implementation 'com.llollox:androidtoggleswitch:2.0.1'
    implementation 'com.android.support:design:28.0.0-beta01'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-    core:3.0.2'
}

这可能是由于我做错了什么引起的,还是我正在使用的某个库有问题。我要加载的文本是一本超过 500 行长的书的片段。

-谢谢

4

0 回答 0