【问题标题】:android data binding error: cannot find symbolandroid数据绑定错误:找不到符号
【发布时间】:2019-12-23 09:16:01
【问题描述】:

我试图在我的主 Activity 中使用 Recycler 和 swipeRefreshLayout,但它就是找不到 RecyclerView。

这是我的 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>

这是我的 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 
        } 
    }

当我尝试编译时它说

找不到符号 public final RecyclerView canadaList;

databinding\ActivityMainBinding.java:27:错误:找不到符号 RecyclerView canadaList, ConstraintLayout constraintLay, SwipeRefreshLayout swipeContainer) {

是不是因为它在我的 activity_main 中的多个层下?如何引用 RecyclerView ?

感谢您的帮助

【问题讨论】:

  • 您的布局中很多地方都在使用 AndroidX,但回收站视图除外。也许您也需要将其更改为 Androidx
  • @Fred 为我的白痴错误道歉!非常感谢它现在可以工作了

标签: java android kotlin data-binding android-databinding


【解决方案1】:

android.support.v7.widget.RecyclerView 更改为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>

【讨论】:

    【解决方案2】:

    对于已经使用 androidx 版本的回收器视图的人,添加依赖项显式帮助。

    implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'
    

    【讨论】:

      猜你喜欢
      • 2019-06-27
      • 2020-05-09
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-02
      • 2015-12-17
      • 1970-01-01
      相关资源
      最近更新 更多