java - 如何解决在 android studio 中查看预览的问题?字符串索引超出范围 : -1

标签 java android android-layout

我看不到这个布局的预览,是唯一有这个问题的,可以构建,其他一切都很好。
拉图

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".PriceActivity">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fabAddCart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/ic_baseline_add_shopping_cart_24" />

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/editCode"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:autofillHints=""
                android:ems="10"
                android:hint="Code"
                android:inputType="number|text" />

            <ImageButton
                android:id="@+id/imgbScan"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:adjustViewBounds="true"
                android:background="@drawable/btn_primary"
                android:cropToPadding="true"
                android:padding="8dp"
                android:scaleType="fitCenter"
                android:tint="#fff"
                app:srcCompat="@drawable/ic_baseline_qr_code_scanner_24" />

        </LinearLayout>

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:visibility="gone"
            tools:srcCompat="@tools:sample/backgrounds/scenic" />

        <TextView
            android:id="@+id/txtPrice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="#2E7D32"
            android:textSize="24sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/txtName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textAlignment="center"
            android:textSize="24sp" />

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
错误信息是:
字符串索引超出范围:-1
细节:
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:325)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:369)
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$6(RenderTask.java:865)
at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout$2.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)
问题是新的,很可能在我更新 android studio 之后,我现在不知道什么时候有这个问题,但只是在这个布局中。应用程序正常工作,但 android studio 中设计 View 的预览和内容树未显示。

最佳答案

问题在于 android:autofillHints="" .删除 android:autofillHints=""来自 EditText并添加 android:importantForAutofill="no" .以上EditText应该如下所示:

        <EditText
        android:id="@+id/editCode"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:importantForAutofill="no"
        android:ems="10"
        android:hint="Code"
        android:inputType="number|text" />
应用上述更改后,请关闭此 xml文件并重新打开它以刷新 View 的呈现。

关于java - 如何解决在 android studio 中查看预览的问题?字符串索引超出范围 : -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67654443/

相关文章:

java - 具有不同 Viewpages 的 fragment

Android Studio 与物理设备断开连接

android - layout_anchor 和 layout_anchorGravity 没有按预期工作

java - Java 9 中 java.se 模块的意义是什么?

java - Jpos 异常 : Can't connect printer device (issue on linux only)

Java Selenium : how can I get the HTML of a webpage without first loading the page?

c# - native Xamarin.Android 应用程序在 Release模式下不断崩溃

android - 如何在 Android 中制作具有多个选项卡/部分的键盘

Android N如何在系统屏幕缩放/显示尺寸更改时拒绝布局缩放

java - Java-Classnames 中前缀 I 的含义