este es el problema que presento en android studio java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blader.domicilios/com.blader.domicilios.activities.client.shopping_bag.ClientShoppingBagActivity}: java.lang.NullPointerException
en el cual cuando intento acceder a la bolsa de compras se me cierra la apliccion o se reinicia, lo mismo sucede cuando intento agregar otros productos y de la misma manera se cierra o se reinicia. necesito de su ayuda ya que estoy cortoo de tiempo para entregarla.
voy a poner el codigo de donde envia el error.
ClientShopingBagActivity
class ClientShoppingBagActivity : AppCompatActivity() {
var recyclerViewShoppingBag: RecyclerView? = null
var textViewTotal: TextView? = null
var buttomNext: Button? = null
var toolbar: Toolbar? = null
var adapter: ShoppingBagAdapter? = null
var sharedPref: SharedPref? = null
var gson = Gson()
var selectedProducts = ArrayList<Product>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_client_shooping_bag)
sharedPref = SharedPref((this))
recyclerViewShoppingBag = findViewById(R.id.recyclerview_shopping_bag)
textViewTotal = findViewById(R.id.textview_total)
buttomNext = findViewById(R.id.btn_next)
toolbar = findViewById(R.id.toolbar)
toolbar?.setTitleTextColor(ContextCompat.getColor(this, R.color.white))
toolbar?.title = "Tu orden"
setSupportActionBar(toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
recyclerViewShoppingBag?.layoutManager = LinearLayoutManager(this)
getProductsFromSharedPref()
}
fun setTotal(total: Double){
textViewTotal?.text = "${total}$"
}
private fun getProductsFromSharedPref(){
if (!sharedPref?.getData("order").isNullOrBlank()){ //SI EXISTE UNA ORDEN EN SHARED PREFERENCE
val type = object: TypeToken<ArrayList<Product>>(){}.type
selectedProducts = gson.fromJson(sharedPref?.getData("order"), type)
adapter = ShoppingBagAdapter(this, selectedProducts)
recyclerViewShoppingBag?.adapter = adapter
}
}
y este es el de ShoppingBagAdapter
class ShoppingBagAdapter(val context: Activity, val products: ArrayList): RecyclerView.Adapter<ShoppingBagAdapter.ShoppingBagViewHolder>() {
val sharedPref = SharedPref(context)
init {
(context as ClientShoppingBagActivity).setTotal(getTotal())
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ShoppingBagViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.cardview_shopping_bag, parent, false)
return ShoppingBagViewHolder(view)
}
override fun getItemCount(): Int {
return products.size
}
override fun onBindViewHolder(holder: ShoppingBagViewHolder, position: Int) {
val product = products[position] // CADA UNA DE LAS CATEGORIAS
holder.textViewName.text = product.name
holder.textViewCounter.text = "${product.quantity}"
holder.textViewPrice.text = "${product.price * product.quantity!!}$"
Glide.with(context).load(product.image1).into(holder.imageViewProduct)
holder.imageViewAdd.setOnClickListener { addItem(product, holder) }
holder.imageViewRemove.setOnClickListener { removeItem(product, holder) }
holder.imageViewDelete.setOnClickListener { deleteItem(position) }
// holder.itemView.setOnClickListener { goToDetail(product) } }
private fun getTotal(): Double {
var total = 0.0
for (p in products) {
total = total + (p.quantity!! * p.price)
}
return total
}
private fun getIndexOf(idProduct: String): Int {
var pos = 0
for (p in products) {
if (p.id == idProduct) {
return pos
}
pos++
}
return -1
}
private fun deleteItem(position: Int) {
products.removeAt(position)
notifyItemRemoved(position)
notifyItemRangeRemoved(position, products.size)
sharedPref.save("order", products)
(context as ClientShoppingBagActivity).setTotal(getTotal())
}
private fun addItem(product: Product, holder: ShoppingBagViewHolder) {
val index = getIndexOf(product.id!!)
product.quantity = product.quantity!! + 1
products[index].quantity = product.quantity
holder.textViewCounter.text = "${product.quantity}"
holder.textViewPrice.text = "${product.quantity!! * product.price}$"
sharedPref.save("order", products)
(context as ClientShoppingBagActivity).setTotal(getTotal())
}
private fun removeItem(product: Product, holder: ShoppingBagViewHolder) {
if (product.quantity!! > 1) {
val index = getIndexOf(product.id!!)
product.quantity = product.quantity!! - 1
products[index].quantity = product.quantity
holder.textViewCounter.text = "${product.quantity}"
holder.textViewPrice.text = "${product.quantity!! * product.price}$"
sharedPref.save("order", products)
(context as ClientShoppingBagActivity).setTotal(getTotal())
}
}
private fun goToDetail(product: Product) {
val i = Intent(context, ClientProductsDetailActivity::class.java)
i.putExtra("product", product.toJson())
context.startActivity(i)
}
class ShoppingBagViewHolder(view: View): RecyclerView.ViewHolder(view) {
val textViewName: TextView
val textViewPrice: TextView
val textViewCounter: TextView
val imageViewProduct: ImageView
val imageViewAdd: ImageView
val imageViewRemove: ImageView
val imageViewDelete: ImageView
init {
textViewName = view.findViewById(R.id.textview_name)
textViewPrice = view.findViewById(R.id.textview_price)
textViewCounter = view.findViewById(R.id.textview_counter)
imageViewProduct = view.findViewById(R.id.imageview_product)
imageViewAdd = view.findViewById(R.id.imageview_add)
imageViewRemove = view.findViewById(R.id.imageview_remove)
imageViewDelete = view.findViewById(R.id.imageview_delete)
}
}