Kotlin:带有火力基础"must not be null"错误的回收视图



我在创建一个android商店应用程序时遇到了很多困难。

我得到一个错误信息

java.lang.IllegalStateException: products_list must not be null

我的回收视图连接到我的firestore,应用程序运行,但随后我得到Logcat错误信息。我正在使用导航抽屉home片段来显示我的RecycleView。

我希望有人能帮助我

HomeActivity.kt

const val TAG: String = "HOMEPAGE_LOG"
class HomeActivity : AppCompatActivity() {
private val firebaseRepo: FirebaseRepo =
FirebaseRepo()
private var productList: List<ProductModel> = ArrayList()
private val productListAdapter: ProductListAdapter = ProductListAdapter(productList)
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_nav_drawer)
val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
products_list.layoutManager  = LinearLayoutManager(this)
products_list.adapter = productListAdapter
if(firebaseRepo.getUser() == null){
// create new user
firebaseRepo.createUser().addOnCompleteListener{
if (it.isSuccessful){
loadProductData()
}else{
Log.d(TAG, "Error:${it.exception!!.message}")
}
}
} else {
//user logged in
loadProductData()
}
products_list.layoutManager  = LinearLayoutManager(this)
products_list.adapter = productListAdapter
val fab: FloatingActionButton = findViewById(R.id.fab)
fab.setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
val navView: NavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
appBarConfiguration = AppBarConfiguration(setOf(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow), drawerLayout)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
}
private fun loadProductData() {
firebaseRepo.getProductList().addOnCompleteListener{
if(it.isSuccessful){
productList = it.result!!.toObjects(ProductModel::class.java)
productListAdapter.productListItems = productList
productListAdapter.notifyDataSetChanged()
}else{
Log.d(TAG, "Error: ${it.exception!!.message}")
}
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.nav_drawer, menu)
return true
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.nav_host_fragment)
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}

您需要用您的回收者视图的id初始化products_list。喜欢的东西:val products_list = findViewById(R.id.product_list)