Android Studio XML 渲染问题 - 字符串索引超出范围:-1

Android Studio XML Render Problem - String Index Out Of Range:-1

当我在 activity 的 XML 代码中添加 android:autoFillHints="" 属性时,设计屏幕突然开始出错。

首先,我编辑的 activity 布局在设计屏幕上没有显示任何内容,只有我编辑的 activity 布局有错误。然后,我试图解决我的一些步骤不起作用(其他都不起作用。):

  1. 我把API版本从30改成29和28,没用。
  2. 我重写了我编辑的 activity 布局中的所有 XML 代码。没用。

我搜索了 Google 和 Stack Over Flow,但没有看到任何解决方法。答案是关于 JavaFX 或字符串索引超出范围的纯 java 代码。

之后,我决定编辑其他 activity 布局,当我编辑其他 activity 布局时开始出现错误。

我收到错误的 XML 文件是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".noteDetails"
    android:paddingHorizontal="20dp"
    android:orientation="vertical">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/ubuntubold"
            android:text="@string/note_details"
            android:textColor="@color/yellow"
            android:textSize="25sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:ignore="MissingConstraints" />

        <ImageButton
            android:id="@+id/saveNoteButton"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/ic_save_note_changes"
            android:visibility="gone"
            android:backgroundTint="@color/green"
            android:layout_marginEnd="20dp"
            app:layout_constraintEnd_toStartOf="@id/deleteNoteButton"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:contentDescription="@string/save_updates_button" />

        <ImageButton
            android:id="@+id/editNoteButton"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/ic_edit_note"
            android:backgroundTint="@color/blue"
            android:layout_marginEnd="20dp"
            app:layout_constraintEnd_toStartOf="@id/deleteNoteButton"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:contentDescription="@string/edit_note_button" />

        <ImageButton
            android:id="@+id/deleteNoteButton"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/ic_delete_note"
            android:backgroundTint="@color/pink"
            app:layout_constraintEnd_toStartOf="@+id/goBackButton"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginEnd="20dp"
            android:contentDescription="@string/delete_note_button" />

        <ImageButton
            android:id="@+id/goBackButton"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/ic_go_back"
            android:backgroundTint="@color/darkblue"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:contentDescription="@string/go_to_main_menu_button" />

    </androidx.constraintlayout.widget.ConstraintLayout>

    <View
        android:layout_width="wrap_content"
        android:layout_height="10dp"
        android:background="@drawable/horizontal_line"/>

    <LinearLayout
        android:id="@+id/editNoteLinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginVertical="20dp"
        android:padding="8dp"
        android:background="@drawable/ic_note_detailed_text_frame">
        
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp">

            <EditText
                android:id="@+id/noteTitleDetailed"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="@font/ubunturegular"
                android:textStyle="bold"
                android:hint="@string/note_title"
                android:background="@null"
                android:inputType="text"
                android:textSize="20sp"
                android:textColor="@color/darkblue"
                android:maxLength="30"
                app:layout_constraintStart_toStartOf="parent"
                tools:ignore="MissingConstraints"
                android:autofillHints="" />

            <View
                android:layout_width="100dp"
                android:layout_height="3dp"
                android:layout_gravity="start"
                android:layout_marginTop="5dp"
                android:background="@drawable/ic_note_title_and_text_divider"
                app:layout_constraintTop_toBottomOf="@id/noteTitleDetailed"
                app:layout_constraintBottom_toBottomOf="parent"
                tools:ignore="MissingConstraints" />

            <TextView
                android:id="@+id/noteDateDetailed"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:fontFamily="@font/ubuntulight"
                android:textSize="12sp"
                android:layout_marginTop="5dp"
                android:text="@string/example_date"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@id/noteTitleDetailed"/>

        </androidx.constraintlayout.widget.ConstraintLayout>

        <EditText
            android:id="@+id/noteTextDetailed"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:fontFamily="@font/ubunturegular"
            android:textSize="16sp"
            android:textColor="@color/darkblue"
            android:hint="@string/note_content"
            android:lines="10"
            android:ems="10"
            android:maxLength="500"
            android:background="@null"
            tools:ignore="MissingConstraints"
            android:layout_marginBottom="10dp"
            android:autofillHints="" />
        
    </LinearLayout>
</LinearLayout>

我控制了一遍又一遍的代码,我没有发现任何关于XML代码的问题。当我编译代码时,程序 运行 没有问题。

错误代码和图像是:

呈现问题错误代码:

java.lang.reflect.InvocationTargetException
    at jdk.internal.reflect.GeneratedMethodAccessor953.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at android.view.BridgeInflater.lambda$createViewFromCustomInflater[=13=](BridgeInflater.java:259)
    at android.view.BridgeInflater.createViewFromCustomInflater(BridgeInflater.java:285)
    at android.view.BridgeInflater.onCreateView(BridgeInflater.java:122)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:928)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:948)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1002)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:309)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1121)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1095)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1124)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1095)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1124)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1095)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:354)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:431)
    at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:710)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate(RenderTask.java:865)
    at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout.run(RenderExecutor.kt:174)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47)
    at java.base/java.lang.String.charAt(String.java:693)
    at android.content.res.BridgeTypedArray.getType(BridgeTypedArray.java:1024)
    at android.content.res.BridgeTypedArray.getType(BridgeTypedArray.java:809)
    at android.content.res.BridgeTypedArray.getValue(BridgeTypedArray.java:778)
    at android.content.res.BridgeTypedArray.peekValue(BridgeTypedArray.java:847)
    at android.view.View.<init>(View.java:5951)
    at android.widget.TextView.<init>(TextView.java:996)
    at android.widget.EditText.<init>(EditText.java:87)
    at android.widget.EditText.<init>(EditText.java:83)
    at androidx.appcompat.widget.AppCompatEditText.<init>(AppCompatEditText.java:93)
    at androidx.appcompat.widget.AppCompatEditText.<init>(AppCompatEditText.java:88)
    at androidx.appcompat.app.AppCompatViewInflater.createEditText(AppCompatViewInflater.java:209)
    at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:127)
    ... 34 more

渲染问题错误图像:

堆栈跟踪错误代码:

java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47)
    at java.base/java.lang.String.charAt(String.java:693)
    at android.content.res.BridgeTypedArray.getType(BridgeTypedArray.java:1024)
    at android.content.res.BridgeTypedArray.getType(BridgeTypedArray.java:809)
    at android.content.res.BridgeTypedArray.getValue(BridgeTypedArray.java:778)
    at android.content.res.BridgeTypedArray.peekValue(BridgeTypedArray.java:847)
    at android.view.View.<init>(View.java:5951)
    at android.view.ViewGroup.<init>(ViewGroup.java:697)
    at android.widget.FrameLayout.<init>(FrameLayout.java:99)
    at com.android.layoutlib.bridge.MockView.<init>(MockView.java:55)
    at com.android.layoutlib.bridge.MockView.<init>(MockView.java:51)
    at com.android.layoutlib.bridge.MockView.<init>(MockView.java:47)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:324)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1121)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1095)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1124)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1095)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1124)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1095)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:354)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:431)
    at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:710)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate(RenderTask.java:865)
    at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout.run(RenderExecutor.kt:174)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)

堆栈跟踪错误图像:

如果有任何遗漏,请告诉我,我会立即添加。从现在开始谢谢你。

答案很简单,只需删除 <EditText/> 中所有 android:autofillHints="" 行即可。并且 XML 渲染再次工作。

只需将任意值添加到 android:autofillHints="" 而不是空白

android:autofillHints=""

android:autofillHints="your hint here"