Search code examples
javaandroidkotlindata-bindingandroid-databinding

android data binding error: cannot find symbol


I am trying to use Recycler and swipeRefreshLayout in my Main Activity but it just cannot find RecyclerView.

This is my activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraintLay"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity"
            tools:showIn="@layout/activity_main">

        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
                android:id="@+id/swipeContainer"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            <android.support.v7.widget.RecyclerView
                    android:id="@+id/canadaList"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:padding="4dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
            />
        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

And this is my MainActivity

class MainActivity : AppCompatActivity() {
        lateinit var swipeContainer: SwipeRefreshLayout 
        lateinit var activityMainBinder : ActivityMainBinding

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)

            activityMainBinder = DataBindingUtil.setContentView(this, R.layout.activity_main)
             // Error here says cannot access RecyclerView 
            val recyclerView = activityMainBinder.canadaList  

            val swipeContainer = activityMainBinder.swipeContainer 
        } 
    }

And when i try to compile it says

cannot find symbol public final RecyclerView canadaList;

In

databinding\ActivityMainBinding.java:27: error: cannot find symbol RecyclerView canadaList, ConstraintLayout constraintLay, SwipeRefreshLayout swipeContainer) {

Is it because it is under multiple layers in my activity_main ? How do I reference RecyclerView ?

Thank you for any help


Solution

  • Change android.support.v7.widget.RecyclerView to androidx.recyclerview.widget.RecyclerView

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/constraintLay"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".MainActivity"
                tools:showIn="@layout/activity_main">
    
            <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
                    android:id="@+id/swipeContainer"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/canadaList"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="8dp"
                        android:layout_marginEnd="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginStart="8dp"
                        android:layout_marginTop="8dp"
                        android:padding="4dp"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                />
            </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
        </androidx.constraintlayout.widget.ConstraintLayout>
    
    </layout>