Home > OS >  Passing Firestore Data Through RecyclerView to Activity
Passing Firestore Data Through RecyclerView to Activity

Time:11-05

I'm trying to pass populated data that I'm getting through Firestore, when I press on a position on a recyclerview, onto another activity. However, I'm getting an error when I try to press on a recyclerview item. Is it because I'm passing intent.getParcelableExtra()!! and I need to pass something else? Thank you!

This is the error:

Caused by: java.lang.NullPointerException
at com.zwdalpha.skedaddle.Activities.Services.CategoryServiceActivity.onCreate(CategoryServiceActivity.kt:32)

PopularCategoriesAdapter.kt

class PopularCategoriesAdapter(val category: ArrayList<Category>) :
    RecyclerView.Adapter<PopularCategoriesAdapter.ViewHolder>() {

    var selectedCategory = Category("", "")

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bindCategory(category[position])

        holder.itemView.setOnClickListener { v ->
            selectedCategory.category = category[position].category
            val intent = Intent(holder.itemView.context, CategoryServiceActivity::class.java)
            intent.putExtra("category", category[position].category)
            holder.itemView.context.startActivity(intent)
        }
    }
}

CategoryServiceActivity.kt

class CategoryServiceActivity : AppCompatActivity() {

    lateinit var category: Category

    @SuppressLint("NotifyDataSetChanged")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_category_service)

// Line 32 - category = intent.getParcelableExtra("category")!!
    }
}

I tried switching the intent.getParcelableExtra("category")!! out with intent.extras!!.get("category") as Category but still getting the error.

CodePudding user response:

Because you are not sending "category" as a object of Category().
Instead you are sending it's category field, i assume it's string.

//intent.putExtra("category", category[position].category)

intent.putExtra("category", category[position]) // use this
  • Related