未解析的引用:当我尝试使用视图绑定访问视图时



我正在开发一个电子商务应用程序,在那里我从一个在线api获取数据。在获得数据后,我正试图将它们放在我的回收者视图中。在这里,我使用ViewBinding将我的xml视图与使用RecyclerView及其适配器的活动绑定在一起。所以问题出现在这里,上面写着对我的视图IDS 的未解决引用

错误:

e: /Users/gama/Desktop/essStore/app/src/main/java/com/example/essstore/view/HotProducts.kt: (25, 21): Unresolved reference: hotProductsScreenProgressBar
e: /Users/gama/Desktop/essStore/app/src/main/java/com/example/essstore/view/HotProducts.kt: (25, 50): Variable expected
e: /Users/gama/Desktop/essStore/app/src/main/java/com/example/essstore/view/HotProducts.kt: (29, 25): Unresolved reference: hotProductsScreenProgressBar
e: /Users/gama/Desktop/essStore/app/src/main/java/com/example/essstore/view/HotProducts.kt: (29, 54): Variable expected
e: /Users/gama/Desktop/essStore/app/src/main/java/com/example/essstore/view/HotProducts.kt: (33, 25): Unresolved reference: hotProductsScreenProgressBar
e: /Users/gama/Desktop/essStore/app/src/main/java/com/example/essstore/view/HotProducts.kt: (33, 54): Variable expected
e: /Users/gama/Desktop/essStore/app/src/main/java/com/example/essstore/view/HotProducts.kt: (43, 21): Unresolved reference: hotProductsScreenProgressBar
e: /Users/gama/Desktop/essStore/app/src/main/java/com/example/essstore/view/HotProducts.kt: (43, 50): Variable expected
e: /Users/gama/Desktop/essStore/app/src/main/java/com/example/essstore/view/HotProducts.kt: (47, 47): Unresolved reference: hotProductsScreenRecyclerView
e: /Users/gama/Desktop/essStore/app/src/main/java/com/example/essstore/view/HotProducts.kt: (49, 9): Unresolved reference: adapter
e: /Users/gama/Desktop/essStore/app/src/main/java/com/example/essstore/view/HotProducts.kt: (50, 9): Unresolved reference: layoutManager

活动

class HotProducts : AppCompatActivity() {
private val TAG = "Hot Products"
private  lateinit var productAdapter: SimpleProductsAdapter
private lateinit var binding: ActivityHotProductsBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityHotProductsBinding.inflate(layoutInflater)
setContentView(binding.root)
setUpRecyclerView()
lifecycleScope.launchWhenCreated {
binding.hotProductsScreenProgressBar.isVisible = true
val response= try {
RetrofitInstance.api.getTodos()
} catch (e: IOException){
binding.hotProductsScreenProgressBar.isVisible = false
Log.e(TAG, "IOException: You might not have internet connection!")
return@launchWhenCreated
}catch (e: HttpException){
binding.hotProductsScreenProgressBar.isVisible = false
Log.e(TAG, "IOException: Unexpected Response!")
return@launchWhenCreated
}
if(response.isSuccessful && response.body()!=null){
productAdapter.products = response.body()!!
}
else{
Log.e(TAG, "IOException: Unexpected Response!")
}
binding.hotProductsScreenProgressBar.isVisible = false
}
}
private fun setUpRecyclerView() = binding.hotProductsScreenRecyclerView.apply{
productAdapter = SimpleProductsAdapter()
adapter = productAdapter
layoutManager = LinearLayoutManager(this@HotProducts)
}
}

这是我的适配器类

package com.example.essstore.data
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.AsyncListDiffer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.example.essstore.databinding.GeneralProductCardBinding
class SimpleProductsAdapter : RecyclerView.Adapter<SimpleProductsAdapter.ProductViewHolder>() {
inner class ProductViewHolder (val binding: GeneralProductCardBinding): RecyclerView.ViewHolder(binding.root)
private val difCallBack= object : DiffUtil.ItemCallback<product>(){
override fun areItemsTheSame(oldItem: product, newItem: product): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: product, newItem: product): Boolean {
return oldItem == newItem
}
}
private val differ = AsyncListDiffer(this, difCallBack)
var products: List<product>
set(value) {
differ.submitList(value)
}
get() = differ.currentList

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
return ProductViewHolder(
GeneralProductCardBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
)
}
override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
holder.binding.apply {
val product = products[position]
generalProductCardTitle.text = product.productName
generalProductCardDescription.text = product.productDescription
generalProductCardPrice.text = "$ ${product.productPrice}"
}
}
override fun getItemCount(): Int {
return products.size
}
}

这是我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/primary_color"
android:orientation="vertical"
android:padding="16dp"
tools:context=".view.HotProducts">
<ImageView
android:id="@+id/_btn_profile_screen_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_back"
android:clickable="true"/>
<TextView
android:layout_marginTop="32dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:fontFamily="@font/montserrat_bold"
android:textSize="24sp"
android:text="Hot Products"
android:textColor="@color/white"/>
<ProgressBar
android:id="@+id/_hot_products_screen_progress_bar"
android:layout_width="match_parent"
android:layout_height="200dp"
android:visibility="gone"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/_hot_products_screen_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/general_product_card"/>
</ScrollView>
</LinearLayout>
</LinearLayout>

请帮帮我!我尝试了很多解决方案,但都不适用。

尝试从XML:中的所有视图id中删除前导下划线

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/primary_color"
android:orientation="vertical"
android:padding="16dp"
tools:context=".view.HotProducts">
<ImageView
android:id="@+id/btn_profile_screen_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_back"
android:clickable="true"/>
...
<ProgressBar
android:id="@+id/hot_products_screen_progress_bar"
android:layout_width="match_parent"
android:layout_height="200dp"
android:visibility="gone"/>
...
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/hot_products_screen_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/general_product_card"/>
...
</LinearLayout>