我使用数据绑定来连接 UI,但是当我重建项目时,出现此错误:
error: cannot find symbol
View root = inflater.inflate(R.layout.toolbar, parent, false);
^
symbol: variable toolbar
location: class layout
ToolbarBinding.java(生成的文件):
<?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>
工具栏.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中,我不知道为什么,请帮我解决这个错误
注释(以防万一您需要): 我使用Android studio Koala 2023.3.2 Canary 2
好吧,您正在尝试通过 layout 而不是 id 来膨胀视图。 如果您正在膨胀,那么您的工具栏必须是存在于另一个布局中的对象才能膨胀。它必须是存在于某处的实际对象,才能动态膨胀。
例如,如果您使用对话框片段:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.appcompat.widget.Toolbar android:id="@+id/toolbar"/>
<TextView android:text="Testing" />
</LinearLayout>
然后要膨胀工具栏,只需执行以下操作:
View root = inflater.inflate(R.id.toolbar, parent, false);