0

My app works fine, it loads data in the recyclerView. But, there is always an error in the Logcat as No adapter attached; skipping layout.

Following is my code, can you tell me where to make what change.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
   
    setHasOptionsMenu(true)

}

onCreateView method

 override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

    binding = FragmentDashboardBinding.inflate(inflater, container, false)
    binding.fbDashboard.setImageResource(R.drawable.ic_grid_view)
    binding.fbDashboard.setOnClickListener {

        if (newView == "ListView") {
            newView = "GridView"
            fb_dashboard.setImageResource(R.drawable.ic_list_view)
        } else {
            newView = "ListView"
            fb_dashboard.setImageResource(R.drawable.ic_grid_view)
        }

        onResume()

    }

    getAppDetails()
    return binding.root


}

The following function is called after getting the data from the Firestore

    fun successDashboardItemsList(dashboardItemsList: ArrayList<Product>) {

    hideProgressDialog()

    if (dashboardItemsList.size > 0) {

        rv_dashboard_items.visibility = View.VISIBLE
        tv_no_dashboard_items_found.visibility = View.GONE

        rv_dashboard_items.layoutManager =
            StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        rv_dashboard_items.setHasFixedSize(true)

        val adapter = DashboardItemsListAdapter(
            requireActivity(),
            dashboardItemsList,
            newView
        )
        rv_dashboard_items.adapter = adapter

        adapter.setOnClickListener(object :
            DashboardItemsListAdapter.OnClickListener {
            override fun onClick(position: Int, product: Product) {

                val intent = Intent(context, ProductDetailsActivity::class.java)
                intent.putExtra(Constants.EXTRA_PRODUCT_ID, product.product_id)
                intent.putExtra(Constants.EXTRA_PRODUCT_OWNER_ID, product.user_id)
                startActivity(intent)
            }
        })

    } else {
        rv_dashboard_items.visibility = View.GONE
        tv_no_dashboard_items_found.visibility = View.VISIBLE
    }

}

EDIT: Before marking this as duplicate and voting down: I have seen many questions like this here, but I am not able to do it as the other posts say. I do not know how to do it, I have to get data from the Firestore to populate the recyclerView. Yet, I tried the following. I tried to add the following code in my onCreate DashFragment.kt But my app crashes with the following error.

val dashboardItemsList: ArrayList<Product> = ArrayList()

    rv_dashboard_items.visibility = View.VISIBLE
    tv_no_dashboard_items_found.visibility = View.GONE

    rv_dashboard_items.layoutManager =
        LinearLayoutManager(context)
    rv_dashboard_items.setHasFixedSize(true)

    val adapter = DashboardItemsListAdapter(
        requireActivity(),
        dashboardItemsList,
        newView
    )

    rv_dashboard_items.adapter = adapter

NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setLayoutManager(androidx.recyclerview.widget.RecyclerView$LayoutManager)' on a null object reference

I am using dataBinding in Kotlin private lateinit var binding: FragmentDashboardBinding

Codist
  • 737
  • 8
  • 23
  • 1
    check the answer to the linked question. this happens if you don't bind adapter to recyclerview as soon as it is attached to the window. – karan Jul 28 '21 at 11:22
  • @karan This is what I have learned and I have already read the same post before. Can you help me to modify my code accordingly? Thanks. – Codist Jul 28 '21 at 11:27

0 Answers0