使用Data Binding重建项目时出现R.layout.toolbar找不到符号错误求助
问题:Data Binding编译报错无法找到
R.layout.toolbar 使用Data Binding进行UI绑定时,重建项目仅在toolbar.xml出现以下编译错误:
error: cannot find symbol View root = inflater.inflate(R.layout.toolbar, parent, false); ^ symbol: variable toolbar location: class layout
使用环境:Android Studio Koala 2023.3.2 Canary 2
相关文件内容
生成文件ToolbarBinding.java(注:实际为XML内容,疑为笔误)
<?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.Toolbar 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:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:minHeight="?attr/actionBarSize" app:layout_scrollFlags="scroll|enterAlways" android:fitsSystemWindows="true" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" tools:ignore="MissingPrefix"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_vertical" android:layout_weight="0.9"> <ImageView android:id="@+id/btn_back" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/ic_chevron_left" android:paddingBottom="10dp" android:clickable="true" android:background="?attr/selectableItemBackgroundBorderless" android:paddingTop="10dp"/> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2"> <TextView android:id="@+id/txt_toolbar" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="Edit Profile" android:textColor="@color/white" android:textStyle="bold" android:textSize="18sp" fontPath="fonts/Proxima_Nova_Bold.otf" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:orientation="horizontal" android:gravity="center" android:layout_weight="1"> <TextView android:id="@+id/btn_save" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:clickable="true" android:text="Save" android:background="?attr/selectableItemBackgroundBorderless" android:textColor="@color/white" android:textSize="16sp" fontPath="fonts/Proxima_Nova_Regular.otf"/> </LinearLayout> </LinearLayout> </androidx.appcompat.widget.Toolbar>
toolbar.xml内容
<?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.Toolbar 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:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:minHeight="?attr/actionBarSize" app:layout_scrollFlags="scroll|enterAlways" android:fitsSystemWindows="true" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" tools:ignore="MissingPrefix"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_vertical" android:layout_weight="0.9"> <ImageView android:id="@+id/btn_back" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/ic_chevron_left" android:paddingBottom="10dp" android:clickable="true" android:background="?attr/selectableItemBackgroundBorderless" android:paddingTop="10dp"/> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2"> <TextView android:id="@+id/txt_toolbar" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="Edit Profile" android:textColor="@color/white" android:textStyle="bold" android:textSize="18sp" fontPath="fonts/Proxima_Nova_Bold.otf" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:orientation="horizontal" android:gravity="center" android:layout_weight="1"> <TextView android:id="@+id/btn_save" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:clickable="true" android:text="Save" android:background="?attr/selectableItemBackgroundBorderless" android:textColor="@color/white" android:textSize="16sp" fontPath="fonts/Proxima_Nova_Regular.otf"/> </LinearLayout> </LinearLayout> </androidx.appcompat.widget.Toolbar>
解决方案
确保Data Binding已启用:检查模块级
build.gradle文件中是否开启Data Binding:android { ... buildFeatures { dataBinding true } }修改布局根标签为
<layout>:Data Binding要求布局必须以<layout>作为根标签包裹,修改toolbar.xml:<?xml version="1.0" encoding="utf-8"?> <layout 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"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:minHeight="?attr/actionBarSize" app:layout_scrollFlags="scroll|enterAlways" android:fitsSystemWindows="true" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" tools:ignore="MissingPrefix"> <!-- 内部布局保持不变 --> </androidx.appcompat.widget.Toolbar> </layout>清理重建项目:依次执行
Build > Clean Project和Build > Rebuild Project,清除旧缓存文件。更新IDE版本:由于使用的是Canary预览版,尝试更新到最新的Koala Canary版本,修复可能存在的构建工具bug。
内容的提问来源于stack exchange,提问作者Muhammad yaumil Ramadhani
相关产品推荐
相关产品推荐

