You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中activity_main.xml渲染报错String索引越界问题

安卓项目activity_main.xml渲染报错解决方案

错误根源

该报错是你使用的Android Studio Arctic Fox 2020.3.1版本的布局渲染器存在兼容缺陷,当XML中的EditText控件设置了空值的android:autofillHints=""属性时,渲染器解析该属性会尝试读取空字符串的字符索引,直接触发String index out of range: -1异常,导致整个布局无法渲染。

解决步骤

第一步:修复EditText的autofillHints属性

找到activity_main.xml里的EditText控件代码,将空的android:autofillHints=""做以下任意一种修改即可:

  • 直接删除该属性(如果不需要自动填充功能)
  • 填入合法的自动填充提示值:
    android:autofillHints="text"
    
  • 保留属性的同时添加忽略警告标记:
    android:autofillHints=""
    tools:ignore="Autofill"
    

修改后的EditText参考代码:

<EditText
    android:id="@+id/userInputET"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:gravity="center_vertical"
    android:hint="@string/write_msg_hint"
    android:inputType="text" />

第二步:可选辅助修复(消除残留渲染警告)

你当前布局里的LinearLayout嵌套在ConstraintLayout中,未添加约束属性会导致布局位置不符合预期,补充如下约束即可:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="18dp"
    android:background="#37009688"
    android:orientation="vertical"
    android:padding="18dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent">

第三步:修改后仍无法渲染可清理缓存

依次点击Android Studio顶部菜单栏的 File → Invalidate Caches...,勾选「Clear file system cache and local history」和「Clear downloaded shared indexes」,点击Invalidate and Restart重启IDE即可。

内容的提问来源于stack exchange,提问作者Cory Eheart

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 08:48:03