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

Android开发:ItemAdapter无法访问自定义XML布局中的TextView问题

问题根源与解决方案

你的核心问题出在list_item.xml的文件结构错误,布局文件不能以<resources>标签作为根元素,这会导致Android编译系统无法正确识别布局内容,进而无法生成对应的R.id.item_title和R.layout.list_item引用,最终引发报错。

步骤1:修正list_item.xml代码

将原文件中的<resources>标签完全移除,直接以TextView作为布局根节点(或包裹在合适的ViewGroup中),正确代码如下:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

如果需要给TextView添加布局约束或内边距,也可以用ViewGroup作为根,比如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp">

    <TextView
        android:id="@+id/item_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

步骤2:清理并重新构建项目

修改布局文件后,执行以下操作确保R文件正确生成:

  • 在Android Studio中点击菜单栏的Build > Clean Project
  • 然后点击Build > Rebuild Project

错误原因说明

<resources>标签是用于定义资源文件(比如strings.xml、colors.xml)的根节点,不能用于布局文件。布局文件的根必须是View或ViewGroup类型的元素(比如TextView、LinearLayout、ConstraintLayout等),否则编译系统无法识别标签类型,就会抛出Can't determine type for tag '<TextView...>'的错误,同时无法生成对应的布局和控件ID引用。

内容的提问来源于stack exchange,提问作者Meet Soni

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 10:03:32