Solution 1 :
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
Problem :
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.
Comments
Comment posted by Alex Mamo
“I’m getting an error” doesn’t provide enough information so we can help. What exactly doesn’t work? What are the exact errors?
Comment posted by Zachary Smouse
I put the error message that I’m getting from the Logcat. I’m getting a NullPointException when I try going to the CategoryServiceActivity