运行Android Studio项目提示cannot find symbol类shape错误如何解决
错误原因
- 资源目录放置错误:你当前的
textbox.xml是Shape Drawable类型的资源文件,应当存放在res/drawable/目录下,但你误将它放在了res/layout/目录中。ViewBinding会默认把layout目录下的所有xml文件识别为界面布局文件,将根标签视为Android View类尝试导入,而android.widget.shape本身是不存在的类,因此触发找不到符号的报错。 - Shape xml存在语法错误:文件中
<stroks标签拼写错误,正确拼写应为<stroke。
解决方案
- 移动资源文件:将
textbox.xml从res/layout/目录剪切到res/drawable/目录下,若项目暂无该目录可手动新建。 - 修正语法错误:调整shape文件的标签拼写,修正后代码如下:
<?xml version="1.0" encoding="utf-8"?> <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <solid android:color="@color/design_default_color_primary"/> <corners android:radius="20dp"/> <stroke android:color="@color/white"/> </shape>
- 重建项目:点击Android Studio顶部菜单栏
Build > Clean Project,执行完成后再点击Build > Rebuild Project,错误的自动生成Binding类会被清理,报错即可解决。 - 补充使用说明:后续需要使用该shape作为控件背景时,直接在布局文件中通过
android:background="@drawable/textbox"引用即可,无需通过ViewBinding加载。
内容的提问来源于stack exchange,提问作者Partha Biswas
相关产品推荐
相关产品推荐

