error: cannot find symbol View root = inflater.inflate(R.layout.toolbar, parent, false);

34 views Asked by At

I used data binding to connect the UI, but when I rebuild the project, I get this error:

error: cannot find symbol
    View root = inflater.inflate(R.layout.toolbar, parent, false);
                                         ^
  symbol:   variable toolbar
  location: class layout

ToolbarBinding.java (generated file) :

<?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>


This error only occurs in toolbar.xml, I don't know why, please help me solve this error

Notes (just in case if you need it) : i used Android studio Koala 2023.3.2 Canary 2

1

There are 1 answers

2
Dan Gerchcovich On

Well, you're trying to inflate a view via a layout instead of id. If you're inflating, then your toolbar needs to be an object that exists inside another layout for it to be inflated. It needs to be an actual object that exists somewhere for it to be inflated dynamically.

For example, if you're using a dialog fragment:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
      <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar"/>
      <TextView android:text="Testing" />
</LinearLayout>

Then to inflate the toolbar, just do this:

View root = inflater.inflate(R.id.toolbar, parent, false);

UPDATE: If you're using the DataBinding library, the data binding library has a Helper class you can use to inflate the components. From there you'd be able to access the root view.

val binder = DataBindingUtil.inflate<NameOfMyBinding>(
            LayoutInflater.from(parent.context),
            R.layout.toolbar,
            parent,
            false
        )

val myRoot = binder.root