微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Kotlin:RecycleView with firebase“不能为空”错误

如何解决Kotlin:RecycleView with firebase“不能为空”错误

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

我收到一条错误消息

java.lang.IllegalStateException: products_list must not be null

我的回收视图已连接到我的 Firestore,该应用程序运行,但随后我收到了 Logcat 错误消息。 我正在使用 Nav drawer 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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。