[英]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. 我正在尝试在我的主要活动中使用Recycler和swipeRefreshLayout,但它只是找不到RecyclerView。
This is my activity_main.xml 这是我的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 这是我的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; 找不到符号公共最终RecyclerView canadaList;
In 在
databinding\\ActivityMainBinding.java:27: error: cannot find symbol RecyclerView canadaList, ConstraintLayout constraintLay, SwipeRefreshLayout swipeContainer) { databinding \\ ActivityMainBinding.java:27:错误:找不到符号RecyclerView canadaList,ConstraintLayout约束层,SwipeRefreshLayout swipeContainer){
Is it because it is under multiple layers in my activity_main ? 是因为它位于我的activity_main中的多层之下吗? How do I reference RecyclerView ? 如何引用RecyclerView?
Thank you for any help 感谢您的任何帮助
Change android.support.v7.widget.RecyclerView
to androidx.recyclerview.widget.RecyclerView
将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>
对于已经使用androidx版本的回收站视图的用户,添加依赖项显然有帮助。
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.